gpt4 book ai didi

asp.net-mvc - EF 6 和 Code First 迁移中同一数据库和应用程序中的多个数据库上下文

转载 作者:行者123 更新时间:2023-12-03 04:59:43 26 4
gpt4 key购买 nike

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

有人可以举一个足够详细的例子,让像我这样的 EF 新人能够理解和使用吗?

最佳答案

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

如果您的项目中有 2 个 DbContext 并且运行 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.

因此,您必须在每个 DbContext 上单独运行 enable-migrations。并且您必须为要生成的每个 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.

关于asp.net-mvc - EF 6 和 Code First 迁移中同一数据库和应用程序中的多个数据库上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21537558/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com