首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >EF 6和Code First迁移中同一数据库和应用程序中的多个数据库上下文

EF 6和Code First迁移中同一数据库和应用程序中的多个数据库上下文
EN

Stack Overflow用户
提问于 2014-02-04 04:55:12
回答 1查看 55.8K关注 0票数 96

我是实体框架的新手。我正在尝试设置一个使用EF6的MVC应用程序。我正在使用Code First Migrations。我正在使用应用程序中的区域,并希望在每个区域有不同的DbContexts来打破它。我知道EF6有ContextKey,但我找不到关于如何使用它的完整信息。目前,我一次只能使用一个上下文的迁移。

有没有人能给出一个足够详细的例子,让像我这样的新人理解和使用EF。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-02-04 05:25:03

实体框架6通过添加-ContextTypeName-MigrationsDirectory标志添加了对多个DbContext的支持。我只是在我的Package Manager控制台中运行命令,并将输出粘贴到下面……

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

代码语言:javascript
复制
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文件指定一个文件夹...

代码语言:javascript
复制
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类的完全限定名称来执行以下操作:

代码语言:javascript
复制
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

代码语言:javascript
复制
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.

希望这能有所帮助。

票数 178
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21537558

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档