在EF 6和代码优先迁移中,同一数据库和应用程序中的多个数据库上下文

 2023-02-17    344  

问题描述

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

有人可以举一个示例,有足够的细节,让一个新人像我这样的人来理解和使用.

在EF 6和代码优先迁移中,同一数据库和应用程序中的多个数据库上下文

推荐答案

实体框架6通过添加-ContextTypeName和-MigrationsDirectory标志,增加了对多个DbContext s的支持.我只是在包装管理器控制台中运行了命令,并粘贴了下面的输出…

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

以上所述是小编给大家介绍的在EF 6和代码优先迁移中,同一数据库和应用程序中的多个数据库上下文,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对77isp云服务器技术网的支持!

原文链接:http://www.77isp.com/post/34274.html

=========================================

http://www.77isp.com/ 为 “云服务器技术网” 唯一官方服务平台,请勿相信其他任何渠道。