多个数据库上下文在同一DB和EF 6和code首先迁移应用 [英] Multiple DB Contexts in the Same DB and Application in EF 6 and Code First Migrations

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

问题描述

我是新来的实体框架。我想设置一个MVC应用程序使用什么EF 6.我使用code首先迁移。我使用领域中的应用,并希望有不同的DbContexts在每个领域打破它。我知道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.

推荐答案

实体框架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 在你的项目,你运行启用的迁移,你会得到一个错误(如你可能已经知道):

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 单独运行启用的迁移每个。你必须指定要生成每个 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 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 ,通过指定的全名,这么做的配置类:

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.

和运行更新数据库以同样的方式:

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.

希望这有助于。

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

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