EF 4.3 Auto-Migrations with multiple DbContexts in one database

前端 未结 9 1726
梦谈多话
梦谈多话 2020-12-01 01:20

I\'m trying to use EF 4.3 migrations with multiple code-first DbContexts. My application is separated into several plugins, which possibly have their own DbContext regarding

9条回答
  •  遥遥无期
    2020-12-01 02:00

    Here is what you can do. very simple.

    You can create Configration Class for each of your context. e.g

    internal sealed class Configuration1 : DbMigrationsConfiguration{
       public Configuration1 (){
            AutomaticMigrationsEnabled = false;
            MigrationsNamespace = "YourProject.Models.ContextNamespace1";
       }
    }
    
    internal sealed class Configuration2 : DbMigrationsConfiguration{
       public Configuration2 (){
            AutomaticMigrationsEnabled = false;
            MigrationsNamespace = "YourProject.Models.ContextNamespace2";
       }
    }
    

    Now you add migration. You dont need to enable migration since you already did with the 2 classed above.

    Add-Migration -configuration Configuration1 Context1Init
    

    This will create migration script for context1. your can repeat this again for other Contexts.

    Add-Migration -configuration Configuration2 Context2Init
    

    To Update your database

    Update-Database -configuration Configuration1
    Update-Database -configuration Configuration2
    

    This can be done in any order. Except you need to make sure each configration is called in sequence.

提交回复
热议问题