Lra*_*ayh 91 asp.net-mvc entity-framework ef-code-first ef-migrations
我是Entity Framework的新手.我正在尝试设置一个使用EF 6的MVC应用程序.我正在使用Code First Migrations.我在应用程序中使用区域,并希望在每个区域中有不同的DbContexts来分解它.我知道EF 6有ContextKey,但我找不到有关如何使用它的完整信息.目前,我一次只能使用一个上下文.
有人可以给出一个足够详细的例子,让我有一个像EF一样的新人来理解和使用.
Ant*_*Chu 172
实体框架6 DbContext通过添加-ContextTypeName和-MigrationsDirectory标志添加了对多个s的支持.我只是在我的软件包管理器控制台中运行命令并粘贴下面的输出...
如果您DbContext的项目中有2 秒并且您运行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.
Run Code Online (Sandbox Code Playgroud)
所以,你必须运行enable-migrations在每个DbContext单独.您必须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.
Run Code Online (Sandbox Code Playgroud)
要为每个迁移添加迁移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.
Run Code Online (Sandbox Code Playgroud)
你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.
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助.