asp.net-mvc EF 6 和 Code First 迁移中同一数据库和应用程序中的多个数据库上下文

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21537558/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 03:30:43  来源:igfitidea点击:

Multiple DB Contexts in the Same DB and Application in EF 6 and Code First Migrations

asp.net-mvcentity-frameworkef-code-firstef-migrations

提问by Lrayh

I'm new to Entity Framework. I am trying to setup an MVC Application what uses EF 6. I am using Code First Migrations. I am using Areas in the app and would like to have different DbContexts in each area to break it up. I know EF 6 has ContextKey, but I can't find complete information on how to use it. Currently I can only use migrations one context at a time.

我是实体框架的新手。我正在尝试设置一个使用 EF 6 的 MVC 应用程序。我正在使用代码优先迁移。我在应用程序中使用区域,并希望在每个区域中使用不同的 DbContext 来分解它。我知道 EF 6 有 ContextKey,但我找不到有关如何使用它的完整信息。目前我一次只能使用迁移一个上下文。

Can someone give an example with enough detail for a new person to EF like me to understand and use.

有人可以举一个足够详细的例子,让像我这样的 EF 新手理解和使用。

回答by Anthony Chu

Entity Framework 6 added support for multiple DbContexts by adding the -ContextTypeNameand -MigrationsDirectoryflags. I just ran the commands in my Package Manager Console and pasted the output below...

实体框架 6DbContext通过添加-ContextTypeName-MigrationsDirectory标志添加了对多个s 的支持。我只是在我的包管理器控制台中运行命令并粘贴下面的输出......

If you have 2 DbContexts in your project and you run enable-migrations, you'll get an error (as you probably already know):

如果您DbContext的项目中有 2 s 并且您运行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.

So you have to run enable-migrationson each DbContextseparately. And you have to specify a folder for each Configuration.csfile to be generated...

所以你必须分别运行enable-migrations每个DbContext。并且您必须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.

To add migrations for each DbContext, you do it like this by specifying the fully qualified name of the Configurationclass:

要为 each 添加迁移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.

And you run update-databasethe same way:

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.

Hope this helps.

希望这可以帮助。