多个数据库上下文在同一DB和EF 6和code首先迁移应用

编程入门 行业动态 更新时间:2024-10-25 02:27:20
本文介绍了多个数据库上下文在同一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首先迁移应用

本文发布于:2023-05-26 16:23:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/265264.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   上下文   数据库   在同一   code

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!