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 :)

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

4 Comments

so, if i use Lazy Loading. How can i design my app to use DBContext object ? Thanks :)
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 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 codeproject.com/Articles/70061/… for example
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.