简体   繁体   中英

Proper way to use DBCONTEXT Class in MVC

In MVC we create Dbcontext Models like

 public class Model1 : DbContext
    {
        public Model1()
            : base("DefaultConnection")
        {}
        public DbSet<SomeObj> SomeObjSet { get; set; }
    }

//another model

 public class Model2 : DbContext
        {
            public Model2()
                : base("DefaultConnection")
            {}
            public DbSet<SomeObj2> SomeObjSet { get; set; }
        }

An then we use controllers like

public class SomeController : Controller
    {
        private Model1 db1 = new Model1();
        private Model2 db2 = new Model2();

        public ActionResult Action1()
        {
            //do sth with Model1 and return
            return View(db1.SomeObjSet.ToList());// 
        }

         public ActionResult Action2()
        {
            //do sth with Model2 and return result
            return View(db2.SomeObjSet.ToList());// 
        }

But my question is that in this way we are creating Multiple DBConnections . Is it better practice to combine the two models into one model and have a dedicated model per controller?

But my question is that isn't that in this way we are creating Multiple DBConnections.

No, don't worry about that. Entity Framework (or more specifically ADO.NET on which it depends) handles actual DB connections in a connection pool. So don't confuse a DbContext with an actual DbConnection. Those are 2 completely different things.

Actually, there is standard way for these type of scenarios..
Unit Of Work & Generic Repository with Entity Framework 5

Also, please visit this post: Understanding and Implementing Repository and Unit of Work Pattern in ASP.NET MVC Application

I am copying few text below from the second link.

Now imagine the scenario where we have multiple tables in the database. Then we need to create multiple repositories in order to map the domain model to the data model. Now having multiple repository classes poses on problem.

The problem is regarding the ObjectContext object. If we create multiple repositories, should they contain their ObjectContext separately? We know that using multiple instances of ObjectContext object simultaneously can be a problem so should we really allow each repository to contain their own instances?

To solve this problem. Why to let each Repository class instance have its own instance of the ObjectContext. Why not create the instance of ObjectContext in some central location and then pass this instance to the repository classes whenever they are being instantiated. Now this new class will be called as UnitOfWork and this class will be responsible for creating the ObjectContext nstance and handing over all the repository instances to the controllers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM