Using Include in Entity Framework 4 with lambda expressions

The RTM version of Entity Framework 4.1 actually includes extension methods in the EntityFramework.dll file, for eager loading with lambda through the Include function. Just include the DLL in your project and you should be able to write code like: var princesses1 = context.Princesses.Include(p => p.Unicorns).ToList(); Remember to add an Import/Using statement to include the … Read more

Repository Pattern, POCO, and Business Entities

First an observation about Asp.net MVC project template I must say that there is a tiny misunderstanding with Visual Studio’s Asp.net MVC project template. And that is the Model folder. People not knowing MVC pattern would automatically relate this to data model and not MVC application/presentation model. This is fine for simple applications where we … Read more

How to implement Unit of Work that works with EF and NHibernate

As a side not building solution supporting different provides on top of the linq is way to disaster. Linq and IQueryable are leaky abstractions – each Linq provider can have its own “features” and limitations. Moreover EF itselfs adds some logic via custom extension methods for IQueryable (like Include or AsNoTracking in EFv4.1). These methods … Read more

Entity Framework: Set Delete Rule with CodeFirst

What you are looking for can be achieved by setting up an optional association between Guest and Language entities: protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<Guest>() .HasOptional(p => p.PreferredLanguage) .WithMany() .HasForeignKey(p => p.LanguageID); } Unit Test: using (var context = new Context()) { var language = new Language() { LanguageName = “en” }; var guest … Read more