EF 6和代码优先迁移中相同数据库和应用程序中的多个数据库上下文

我是entity framework的新手。 我想设置一个MVC应用程序什么使用EF 6.我正在使用Code First Migrations。 我在应用程序中使用区域,并希望在每个区域有不同的DbContexts分解。 我知道EF 6有ContextKey,但我找不到有关如何使用它的完整信息。 目前,我一次只能使用一种上下文迁移。

有人能给我一个足够的细节给一个新的人来EF像我一样理解和使用。

entity framework6通过添加-ContextTypeName-MigrationsDirectory标志添加了对多个DbContext的支持。 我只是在我的软件包pipe理器控制台中运行命令并粘贴下面的输出…

如果您的项目中有2个DbContext ,并且运行了enable-migrations ,则会出现错误(您可能已经知道):

 PM> enable-migrations More than one context type was found in the assembly 'WebApplication3'. To enable migrations for 'WebApplication3.Models.ApplicationDbContext', use Enable-Migrations -ContextTypeName WebApplication3.Models.ApplicationDbContext. To enable migrations for 'WebApplication3.Models.AnotherDbContext', use Enable-Migrations -ContextTypeName WebApplication3.Models.AnotherDbContext. 

所以你必须分别在每个DbContext上运行enable-migrations 。 而且你必须为每个Configuration.cs文件指定一个文件夹来生成…

 PM> Enable-Migrations -ContextTypeName ApplicationDbContext -MigrationsDirectory Migrations\ApplicationDbContext Checking if the context targets an existing database... Code First Migrations enabled for project WebApplication3. PM> Enable-Migrations -ContextTypeName AnotherDbContext -MigrationsDirectory Migrations\AnotherDbContext Checking if the context targets an existing database... Code First Migrations enabled for project WebApplication3. 

要为每个DbContext添加迁移,可以通过指定Configuration类的完全限定名称来完成:

 PM> Add-Migration -ConfigurationTypeName WebApplication3.Migrations.ApplicationDbContext.Configuration "InitialDatabaseCreation" Scaffolding migration 'InitialDatabaseCreation'. The Designer Code for this migration file includes a snapshot of your current Code First model. This snapshot is used to calculate the changes to your model when you scaffold the next migration. If you make additional changes to your model that you want to include in this migration, then you can re-scaffold it by running 'Add-Migration InitialDatabaseCreation' again. PM> Add-Migration -ConfigurationTypeName WebApplication3.Migrations.AnotherDbContext.Configuration "InitialDatabaseCreation" Scaffolding migration 'InitialDatabaseCreation'. The Designer Code for this migration file includes a snapshot of your current Code First model. This snapshot is used to calculate the changes to your model when you scaffold the next migration. If you make additional changes to your model that you want to include in this migration, then you can re-scaffold it by running 'Add-Migration InitialDatabaseCreation' again. 

而且你以同样的方式运行update-database

 PM> Update-Database -ConfigurationTypeName WebApplication3.Migrations.ApplicationDbContext.Configuration Specify the '-Verbose' flag to view the SQL statements being applied to the target database. Applying explicit migrations: [201402032113124_InitialDatabaseCreation]. Applying explicit migration: 201402032113124_InitialDatabaseCreation. Running Seed method. PM> Update-Database -ConfigurationTypeName WebApplication3.Migrations.AnotherDbContext.Configuration Specify the '-Verbose' flag to view the SQL statements being applied to the target database. Applying explicit migrations: [201402032113383_InitialDatabaseCreation]. Applying explicit migration: 201402032113383_InitialDatabaseCreation. Running Seed method. 

希望这可以帮助。