Entity Framework, CTP5, SQLServerCompact, missing Database.SetInitializer - entity-framework-4.1

I've installed EFCodeFirst 0.8 (CTP5) and EFCodeFirst.SQLServerCompact 0.8.84821. I've updated the references to EntityFramework and System.Data.SqlServerCe.Entity.dll.
My project compiles and runs, with one exception: I can not call Database.SetInitializer.
Here is how my code looks:
using System.Data.Entity;
namespace Recipes.Models
{
public class RecipesEntities : DbContext
{
public RecipesEntities()
{
System.Data.Entity.Database.SetInitializer<RecipesEntities>(new SampleData());
//Database.SetInitializer<RecipesEntities>(new SampleData());
}
public DbSet<Category> Categories { get; set; }
...
}
}
and the error I get is
The type or namespace 'SetInitializer' does not exist in the namespace
'System.Data.Entity.Database
Indeed, it does not show up in the Intellisense. However, as I said - if I comment out the line that calls SetInitializer, the project compiles and runs. I can not test it properly though cause most operations depend on some seed data in the DB.
What could be the reason for such behaviour?

Related

system.invalidoperationexception sequence contains no elements at system.linq.enumerable.first[TSource]{IEnumaberable`1 source}

I installed an updated visual studio in 2019. After that, I opened my xamarin application and ran the app. It's built successfully. Before opening the application in the emulator I got the below issue.
I was helped by the exclusion of using an inherited classes without overridden methods.
for example base class defined in net standard lib:
public class DFStorage
{
public virtual bool SaveAppTextFile(...)
{
...
}
public virtual string GetAppTextFile(...)
{
...
}
}
inherited in platform specific lib:
(without any overrides)
public class DFStorageIOS : DFStorage
{
}
App use platform class.
Overriding of one method was enough:
public class DFStorageIOS : DFStorage
{
public override bool SaveAppTextFile(...)
{
return base.SaveAppTextFile(...);
}
}

FunctionIndexingException Can't bind Table to type on Azure WebJob Load

I think this is a case of out of date documentation but I'm unable to find a more recent example.
Using the following code generates an exception on the initialisation of the webjob and it gets stuck in a "pending restart" loop.
public static void GenerateExcelFile(
[QueueTrigger("excel")] JobFile message,
Guid Id,
[Table("JobFile")] IDictionary<Tuple<string, string>, object> table,
{
//More Code
}
Replacing "object" with "JobFile" produces the same error. It's quite a length stacktrace so I've only posted the top of it here. Using ILSpy it looks like this shouldn't work so I'm not sure if this feature has been removed since the tutorial was written.
[09/13/2014 11:07:53 > be5c40: ERR ] Unhandled Exception:
Microsoft.Azure.WebJobs.Host.Indexers.FunctionIndexingException:
Error indexing method 'GenerateExcelFile' --->
System.InvalidOperationException: Can't bind Table to type
'System.Collections.Generic.IDictionary`2[System.Tuple`2[System.String,System.String],System.Object]'.
[09/13/2014 11:07:53 > be5c40: ERR ] at Microsoft.Azure.WebJobs.Host.Tables.TableAttributeBindingProvider.TryCreateAsync(BindingProviderContext context)
[09/13/2014 11:07:53 > be5c40: ERR ] at Microsoft.Azure.WebJobs.Host.Bindings.CompositeBindingProvider.<TryCreateAsync>d__0.MoveNext()
I've tried this on 0.5 beta and 0.6 beta of the SDK.
The documentation that you are pointing to is out of date. IDictionary binding was removed for Tables. You can use ICollector binding for Inserting or replacing, TableEntity/ IQueryable binding for reading and CloudTable binding for modifying an entity.
The following samples demonstrate Tables usage
https://github.com/Azure/azure-webjobs-sdk-samples/tree/master/BasicSamples/TableOperations
https://github.com/bradygaster/siteMonitR
As I had to search a while to find how to use the ICollector binding, I thought I'd share.
It looks like it belongs to a new release in Microsoft.Azure.WebJobs so make sure you are using the version 0.6.0-beta.
In your case it would be something like
public static void GenerateExcelFile(
[QueueTrigger("excel")] JobFile message,
Guid Id,
[Table("JobFile")] ICollector<JobFile> tableBinding
{
//More Code
}
public class JobFile
{
public string PartitionKey { get; set; }
public string RowKey { get; set; }
public string Name { get; set; }
}
P.S. I have not tested this! :P
See the link for details
http://blogs.msdn.com/b/webdev/archive/2014/09/12/announcing-the-0-6-0-beta-preview-of-microsoft-azure-webjobs-sdk.aspx

Setting lifetime manager for registrations done using UnityConfiguration scanner

I have a ASP.NET MVC4 application and am using Unity for IOC. I am using Unity.MVC4 and UnityConfiguration Nuget packages to help with the registration.
I need to automatically register a load of interfaces and their related types to the Unity container. To do this I created a dummy interface; IDependencyInjectionScanner that all my real interfaces inherit from. Below is the code showing that.
public interface IDependencyInjectionScanner
{
}
public interface IChair : IDependencyInjectionScanner
{
NumberOfLegs { get; set; }
}
public class Chair : IChair
{
public NumberOfLegs { get; set; }
}
public interface ITable : IDependencyInjectionScanner
{
NumberOfChairs { get; set; }
}
public class Table : ITable
{
public NumberOfChairs { get; set; }
}
I then used UnityConfiguration to bind the registrations using the scanner. I have get the interfaces being correctly resolved in the controller. Below is the code that shows how I did the binding.
Scan(scan =>
{
scan.AssembliesInDirectory(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "bin"));
scan.With<FirstInterfaceConvention>();
scan.Include(x => (x.GetInterface(typeof(IDependencyInjectionScanner).Name) != null));
scan.ForRegistries();
});
The problem is that I want to register all the types found by the scanner using the hierarchical lifetime manager but can figure out how to do this. The GitHub page for UnityConfiguration https://github.com/thedersen/UnityConfiguration states that this could be achieved by the code below:
Configure<IChair>().AsHierarchicalControlled();
However I if I have to do that for each of the interfaces bound by the scanner then the scanner is of no use as I may as well do:
Register<IChair, Chair>().AsHierarchicalControlled();
Can someone assist me with finding a solution to this please.
Here's an answer to your question using UnityConfiguration. You can create a custom convention to configure the lifetime. Just be careful because it looks like the calls within the Scan() method are order dependent.
public class HierarchicalLifetimeConvention : IAssemblyScannerConvention
{
public void Process(Type type, IUnityRegistry registry)
{
registry.Configure(type).AsHierarchicalControlled();
}
}
and then add that to your Scan() call...
Scan(scan =>
{
scan.AssembliesInDirectory(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "bin"));
scan.With<FirstInterfaceConvention>();
scan.With<HierarchicalLifetimeConvention>(); //<-- New convention
scan.Include(x => (x.GetInterface(typeof(IDependencyInjectionScanner).Name) != null));
scan.ForRegistries();
});
As suggested by #TylerOhlsen I used the built-in Registration by Convention feature of Unity 3.0. I have got it to add the registration mappings and they are using the hierarchical lifetime manager. below is the code for that
container.RegisterTypes(
AllClasses.FromLoadedAssemblies().Where(
t => t.GetInterface(typeof(IDependencyInjectionScanner).Name) != null),
WithMappings.FromMatchingInterface,
WithName.Default,
WithLifetime.Hierarchical);
There is one thing that is disturbing me; when I look at the registrations I have 4 (based on the example code above). 2 type mappings for the Chair type and 2 type mappings for the Table type.
Can anyone shed any light on why this is, as I was only expecting two mappings.

SQL Server CE identifies a cyclical reference with Entity Framework Code First but SQL Server 2008 does not

I am working on an Entity Framework Code First project that has a fairly complex Data Model which deploys absolutely fine on SQL Server 2008.
However when creating an SQL Server CE database for some local End-To-End testing I get the following error message when EF creates the database:
System.Data.SqlServerCe.SqlCeException: The referential relationship will result in a cyclical reference that is not allowed. [ Constraint name = FK_Sites_Persons_PersonId ].
I have disabled the ManyToManyCascadeDeleteConvention in my DataContext model creation method, so that isn't the cause of the problem. The trouble I have is that the relationship in question looks fine in the SQL Server 2008 database- it appears to be a normal foreign key from what I can tell and I can't see anything flowing back in the other direction, although it is not impossible that there is a longer-path circular reference. I don't know why CE would fail and 2008 would succeed.
It turns out the problem was very simply solved- although I had disabled ManyToManyCascadeDeleteConvention I also needed to disable the OneToManyCascadeDeleteConvention to avoid the circular reference problem.
You might also consider explicitly defining the cascading updates and deletes rather than disabling them globally. Assume a model:
namespace Models
{
public class Parent
{
public Parent() { this.Children = new HashSet<Child>(); }
public int id { get; set; }
public string description { get; set; }
public ICollection<Child> Children { get; set; }
}
public class Child
{
public int id { get; set; }
public string description { get; set; }
public Parent Parent { get; set; }
}
}
Override the OnModelCreating in your context and use the fluent api to specify the cascade options for a given relationship.
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Parent>().HasMany<Child>(p => p.Children).WithRequired(c => c.Parent).WillCascadeOnDelete(false);
base.OnModelCreating(modelBuilder);
}
Of course this is a simple example, but you can apply the same principle to your down-level entities and specifically exclude the cascaded delete that causes the circular reference.

The type or namespace name 'MicroKernel' does not exist in the namespace 'Castle' (are you missing an assembly reference?)

I'm new to castle windsor and wanted to learn it.
I downloaded Windsor 2.5.3 for .net4 from here http://www.castleproject.org/castle/download.html
I built my first console app using vs2010 and try to play around.
The following are my code(very simple)
using Castle.Windsor;
using Castle.MicroKernel.Registration;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
WindsorContainer wc = new WindsorContainer();
wc.Register(Component.For<I>().ImplementedBy<C>());
var v = wc.Resolve<I>();
var result = v.M();
}
}
public class C : I
{
public string P1 { get; set; }
public int M()
{
return 100;
}
}
public interface I
{
int M();
}
}
But it didn't get compiled, error msg says:
The type or namespace name 'MicroKernel' does not exist in the namespace 'Castle' (are you missing an assembly reference?)
The type or namespace name 'Windsor' does not exist in the namespace 'Castle' (are you missing an assembly reference?)
I actually referenced castle.core and castle.windsor dlls and intellisense was working fine until compile....
I also noticed that when I double click the castle.windsor in reference, it's not showing the namespace hierarchy in object browser window.
I even commented out all my code, it still can't compile, says the same error msg.
Can you please advise what can I do to make it run. really appreciate it!!
The problem is likely the target framework of your project.
Open project properties, and look for the target framework dropdown. If it says .Net Framework 4.0 Client Profile, change it to .Net Framework 4.0.