Asp.net-Mvc
EF 6 和程式碼優先遷移中的同一數據庫和應用程序中的多個數據庫上下文
我是實體框架的新手。我正在嘗試設置使用 EF 6 的 MVC 應用程序。我正在使用 Code First 遷移。我在應用程序中使用區域,並希望在每個區域中使用不同的 DbContext 來分解它。我知道 EF 6 有 ContextKey,但我找不到有關如何使用它的完整資訊。目前我一次只能使用遷移一個上下文。
有人可以舉一個足夠詳細的例子,讓像我這樣的 EF 新手能夠理解和使用。
Entity Framework 6通過添加and標誌
DbContext增加了對多個 s 的支持。我剛剛在我的包管理器控制台中執行命令並將輸出粘貼到下面…-ContextTypeName``-MigrationsDirectory如果你
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.所以你必須分別執行
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.要為每個添加遷移
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.希望這可以幫助。