K.Z
K.Z

Reputation: 5075

got issue with using multiple dbContext in ASP.NET MVC 5

I am struggling with using multiple dbContext with an single web application in ASP.NET MVC 5. I am following code First existing database design approach.

so i have created dashboardModel using ADO.NET Entity model, that comes with its own dbContext (DashboardContext) and then roleModel using again ADO.net Entity Model (dbContext = RoleContext).

I want to keep similar concern of model separtate and their individual DBContext.

On creating DashboardModel, code run without problem but when i have created RoleModel and run; it gives me error on Dashboard controller ==> MetadataException was unhandled by user code

public DashboardContext()
        : base("name=DashboardContext")
    {
    }

////

    public class DashboardController : Controller
{
    //
    // GET: /Dashboard/
    public ActionResult Home()
    {
        using (var db = new DashboardContext())
        {
            var query = from b in db.sys_Functions
                        orderby b.Function_ID
                        select b;

            foreach(var item in query)
            {
                var a1 = item.Title;
            }
        }

        return View();
    }
}

//

 public RoleContext()
        : base("name=RoleContext")
    {
    }

//

 public class TestController : Controller
{
    //
    // GET: /Test/
    public ActionResult Index()
    {
        using(var db = new RoleContext())
        {
            var query = from x in db.AspNetRoles
                        orderby x.Name
                        select x;

            foreach(var item in query)
            {
                var t = item.Name;
            }
        }

        return View();
    }
}

Many Thanks

Upvotes: 0

Views: 907

Answers (2)

Adel Mourad
Adel Mourad

Reputation: 1547

Note: this may not be so related to the question

I wanted to make two ApplicationDbContext with two different connections in ASP.NET MVC 5 (not ASP.NET CORE)

This is what worked for me, first you will need to add a second / overloaded constructor to ApplicationDbContext

Then add another ApplicationDbContext class like "ReadOnly_1_Local_ApplicationDbContext" that inherits the orignial ApplicationDbContext not the "IdentityDbContext" and make its constructor calls the overloaded base constructor with your new connection

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext() : base("<your connection>", throwIfV1Schema: false)
    {

    }

    public ApplicationDbContext(string nameOrConnectionString) : base(nameOrConnectionString, throwIfV1Schema: false)
    {

    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }
}


public class ReadOnly_1_Local_ApplicationDbContext : ApplicationDbContext
{
    public ReadOnly_1_Local_ApplicationDbContext() : base("<your read only connection>")
    {

    }
}

Usage (I stopped sync from the first db to the read only one and changed the data in one of them to see it really connects to the other db)

public ActionResult TestMultiDbContext_1()
{
    var db_context = new ApplicationDbContext();
    return Content(db_context.Shipments.Find(123456).CustomerName); // name #1
}

public ActionResult TestMultiDbContext_2()
{
    var db_context = new ReadOnly_1_Local_ApplicationDbContext();

    return Content(db_context.Shipments.Find(123456).CustomerName); //name #2
}

That is tested with code first migrations and does not cause any issue and is not generating any extra migrations.

If you are adding another connection for entirely new db (not duplicated one), just make another class that inherits the IdentityDbContext normally

Thanks

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

I want to keep similar concern of model separtate and their individual DBContext.

DbContext is the abstraction for a database. So unless you are connecting your entities to different databases, there's no reason to use different Db contexts.

Upvotes: 2

Related Questions