Entity Framework 4.3 code first multiple many to many using the same tables

Well, EF doesn’t have some kind of word and grammar recognition algorithm which would be required to identify that you (probably) want that User.Residencies and Town.Residents form a pair of navigation properties and User.Mayorships and Town.Mayors form a second pair. Therefore it assumes that you have four one-to-many relationships and that each of the four … Read more

Linq when using GroupBy, Include is not working

Include demands that the shape of the query doesn’t change. It means that your query must return IQueryable<Match>. GroupBy operator is probably considered as shape changing because it returns IQueryable<IGrouping<TKey, TSource>>. Once the shape of the query changes all Include statements are omitted. Because of that you cannot use Include with projections, custom joins and … Read more

How to fix: The number of properties in the Dependent and Principal Roles in a relationship constraint must be identical?

The reason for the error are incorrectly configured relations in your model. This is not correct: one.HasRequired(t => t.Two) .WithMany(s => s.Ones) .HasForeignKey(t => t.TwoId); one.HasRequired(t => t.Three) .WithMany(s => s.Ones) .HasForeignKey(t => t.ThreeId); It should be: one.HasRequired(t => t.Two) .WithMany(s => s.Ones) .HasForeignKey(t => new { t.TwoId, t.ThreeId }); Because dependent’s FK must contain … Read more

Entity Framework initialization is SLOW — what can I do to bootstrap it faster?

In pre EF6 view generation is known to be slow for bigger models. For now the solution is to use pregenerated views. This way you generate views at design time and are avoiding this work at runtime. To do that download EF power tools and select “Optimize Entity Data Model”. It will add a C# … Read more

Best way to incrementally seed data in Entity Framework 4.3

If you want to use entities to seed data you should use Seed method in your migrations configuration. If you generate fresh project Enable-Migrations you will get this configuration class: internal sealed class Configuration : DbMigrationsConfiguration<YourContext> { public Configuration() { AutomaticMigrationsEnabled = false; } protected override void Seed(CFMigrationsWithNoMagic.BlogContext context) { // This method will be … Read more

Confusion over EF Auto Migrations and seeding – seeding every program start

The fact that the Seed method ran only when the database changed was quite limiting for the database initializers that shipped in EF 4.1. It was limiting because sometimes you needed to update seed data without changing the database, but to make that happen you had to artificially make it seem like the database had … Read more