EF 6 和 Code First 迁移中同一 DB 和应用程序中的多个 DB 上下文 [英] Multiple DB Contexts in the Same DB and Application in EF 6 and Code First Migrations

查看:26
本文介绍了EF 6 和 Code First 迁移中同一 DB 和应用程序中的多个 DB 上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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新手理解和使用.

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

推荐答案

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

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

如果你的项目中有 2 个 DbContext 并且你运行 enable-migrations,你会得到一个错误(你可能已经知道了):

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

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 文件指定一个文件夹...

So you have to run enable-migrations on each DbContext separately. And you have to specify a folder for each Configuration.cs file to be generated...

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

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

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

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

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:

And you run update-database the same way:

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.

希望这会有所帮助.

这篇关于EF 6 和 Code First 迁移中同一 DB 和应用程序中的多个 DB 上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆