简体   繁体   中英

EF Code-First from existing db add models into existing DbContext

So maybe this is a stupid question, because I know that when creating a Code-first model from an existing database, Visual Studio will create a new ADO.NET Entity Data Model, and add the models in a new DbContext.

I am using Microsoft Identity in my project, hence there is already a ApplicationDbContext (IdentityDbContext). I think just having all my models in a single DbContext would be easier to handle. I am generating my code-first models from an existing database.

But Is there a way such that the generated models add up into the already existing DbContext (In this case, the IdentityDbContext?)

I have like, many models, so currently I am compelled to add each of them into existing ApplicationDbContext manually, and remove from the created DbContext.

As far as I remember there is no way to add the generated models objects to the existing DbContext automatically. You need to add them manually.

public partial class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("name=DefaultConnection")            
    {

    }

    //Add your Model objects here, You can copy them from automatically generated DbContext
    public virtual DbSet<ModelObjectName> PropertyName { get; set; }


    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        //Copy the modelBuilder configuration here from automatically generated DbContext here

        base.OnModelCreating(modelBuilder);
    }
}

Here's an alternative that works:

  1. Mark your ApplicationDbContext class as partial:

     public partial class ApplicationDbContext : IdentityDbContext<ApplicationUser> { public ApplicationDbContext() : base("DefaultConnection") { //unchanged } protected override void OnModelCreating(DbModelBuilder modelBuilder) { //unchanged } } 
  2. Do the same with your custom Data model class, and remove the inheritance from DbContext . Also, remove constructor form it and change the OnModelCreating into a regular private method with some different name. Keep the rest unchanged.

     public partial class MyDataModels { //unchanged private void OnModelCreating2(DbModelBuilder modelBuilder) { //unchanged } } 
  3. Refactor ( Ctrl + R + R : default shortcut ) name of your Data model class, and change it to ApplicationDbContext . Visual Studio might give you a conflict warning during refactoring, ignore that and refactor. Finally, call OnModelCreating2() from OnModelCreating() method of ApplicationDbContext class.

     public partial class ApplicationDbContext : IdentityDbContext<ApplicationUser> { public ApplicationDbContext() : base("DefaultConnection") { //unchanged } protected override void OnModelCreating(DbModelBuilder modelBuilder) { //unchanged OnModelCreating2(modelBuilder); } } 

Another nice approach suggested by @DevilSuichiro is to simply inherit your Data model class from ApplicationDbContext .

Cheers!

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