1

I'm sorry if my question is normal. But I meet this problem when I design my ASP.NET MVC 4.0 Application using Entity Framework 5.

If I choose Eager Loading, I just simplify using :

public Problem getProblemById(int id) {
 using(DBEntity ctx = new DBEntity ())
            {
                return (Problem) ctx.Posts.Find(id);
            }
}

But if I use Eager Loading, I will meet problem: when I want to navigate through all its attributes such as comments (of problem), User (of Problem) ... I must manually use Include to include those properties. and At sometimes, If I don't use those properties, I will lost performance, and maybe I lost the strength of Entity Framework.

If I use Lazy Loading. There are two ways to use DBContext object. First way is using DBContext object locally :

public Problem getProblemById(int id) {
 DBEntity ctx = new DBEntity ();
 return (Problem) ctx.Posts.Find(id);
}

Using this, I think will meet memory leak, because ctx will never dispose again.

Second way is make DBContext object static and use it globally :

static DBEntity ctx = new DBEntity ();
public Problem getProblemById(int id) {
 return (Problem) ctx.Posts.Find(id);
}

I read some blog, they say that, if I use this way, I must control concurrency access (because multi request sends to server) by myself, OMG. For example this link :

Entity Framework DBContext Usage

So, how can design my app, please help me figure out.

Thanks :)

hqt
  • 29,632
  • 51
  • 171
  • 250

1 Answers1

8

Don't use a static DBContext object. See c# working with Entity Framework in a multi threaded server

A simple rule for ASP.Net MVC: use a DBContext instance per user request.

As for using lazy loading or not, I would say it depends, but personally I would deactivate lazy-loading. IMO it's a broken feature because there are fundamental issues with it:

  • just too hard to handle exceptions, because a SQL request can fail at any place in your code (not just in the DAL because one developer can access to a navigation property in any piece of code)
  • poor performances if not well used
  • too easy to write broken code that produces thousands of SQL requests
Community
  • 1
  • 1
ken2k
  • 48,145
  • 10
  • 116
  • 176
  • so, if i use Lazy Loading. How can i design my app to use DBContext object ? Thanks :) – hqt Jan 27 '14 at 11:27
  • I see many posts (include MSDN link) that recommend not to use lazy loading. So, why microsoft still have this choices for programmers :( and they don't make any changes to make it easier to use :( – hqt Jan 27 '14 at 11:46
  • @hqt Using lazy loading or not won't change the way you use DBContext itself ; you'll still have to find a way to create a new instance per user request. A simple approach is to instantiate DBContext in the constructor of each controller, and pass it to the business layer. A more robust approach would be to use a IoC framework to automatically inject an instance of the context in each controller constructor using dependency injection. See http://www.codeproject.com/Articles/70061/Architecture-Guide-ASP-NET-MVC-Framework-plus-N-ti for example – ken2k Jan 27 '14 at 12:53
  • 1
    @hqt For the "why lazy loading is still a choice?" question, probably because it's easy to use a first glance and it looks powerful in all those "hello world" demos. But a lot of problems come quickly in a real world application because of it. I'm sure it's possible to use it correctly, but as a general rule, I would recommend to **not** use it unless you really know how it internally works. – ken2k Jan 27 '14 at 12:57