One DbContext per request in ASP.NET MVC (without IOC container)

I know this is not a recent question, but I’ll post my answer anyway, because I believe someone may find it useful. As probably many others, I followed the steps mentioned in the accepted answer. Yay, it works. HOWEVER, there’s one catch: Methods BeginRequest() and EndRequest() fire each time a request is made, but not … Read more

Entity Framework Join 3 Tables

I think it will be easier using syntax-based query: var entryPoint = (from ep in dbContext.tbl_EntryPoint join e in dbContext.tbl_Entry on ep.EID equals e.EID join t in dbContext.tbl_Title on e.TID equals t.TID where e.OwnerID == user.UID select new { UID = e.OwnerID, TID = e.TID, Title = t.Title, EID = e.EID }).Take(10); And you should … Read more

The relationship could not be changed because one or more of the foreign-key properties is non-nullable

You should delete old child items thisParent.ChildItems one by one manually. Entity Framework doesn’t do that for you. It finally cannot decide what you want to do with the old child items – if you want to throw them away or if you want to keep and assign them to other parent entities. You must … Read more

What effect(s) can the virtual keyword have in Entity Framework 4.1 POCO Code First?

So far, I know of these effects. Lazy Loading: Any virtual ICollections will be lazy-loaded unless you specifically mark them otherwise. More efficient change tracking. If you meet all the following requirements then your change tracking can use a more efficient method by hooking your virtual properties. From the link: To get change tracking proxies, … Read more

Unique key with EF code first

Unfortunately you can’t define it as unique key in code first because EF doesn’t support unique keys at all (it is hopefully planned for next major release). What you can do is to create custom database intializer and add unique index manually by calling SQL command: public class MyInitializer : CreateDatabaseIfNotExists<MyContext> { protected override void … Read more

Generic Repository With EF 4.1 what is the point

You are actually right. DbContext is an implementation of the unit of work pattern and IDbSet is an implementation of the repository pattern. Repositories are currently very popular and overused. Everybody use them just because there are dozens of articles about creating repository for entity framework but nobody actually describes challenges related to this decision. … Read more