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


94

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

有人可以提供一个足够详细的示例,让像我这样的EF新手了解和使用。

Answers:


176

实体框架6 DbContext通过添加-ContextTypeName-MigrationsDirectory标志,增加了对Multiple 的支持。我只是在程序包管理器控制台中运行了命令,然后将输出粘贴到下面...

如果您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.

因此,您必须分别运行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.

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

希望这可以帮助。


我是否必须为每个上下文使用单独的连接字符串,或者有没有解决的办法?
Lrayh 2014年

3
它们可以共享相同的连接字符串。但您要确保它们不会映射到相同的表。
Anthony Chu

如果它们确实映射到同一个表,则仍然可以定义哪个迁移将首先运行,并让其迁移文件创建该表,然后哪个迁移文件将运行并对其进行修改,以使其不创建已经存在的表。然后,您可以使用每个上下文的MigrateDatabaseToLatestVersionforze强制ctx.Database.initialize()以正确的顺序运行,或者Update-Database以正确的顺序手动运行命令。(相反,如果您将数据库迁移到以前的版本,则相反)。这是“危险的”,但是可以做到的。
JotaBe 2014年

因此,我向项目添加了迁移,并创建了与ApplicationDbContext不同的上下文。我继续使用了大约6个月的与网站相关的数据,然后就该开始搞乱我的ApplicationUser了。我的基本登录和注册工作正常,但是我想扩展用户类以添加一些其他字段。这个答案对于为该上下文设置新的迁移配置非常有帮助。谢谢!#1up
Eric Bishard

1
如果我能为您提供+10的简短答复,但我的答案已经足够了,谢谢@AnthonyChu。
Karim AG
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.