簡體   English   中英

IdentityDbContext如何配置遷移

[英]IdentityDbContext how to configure migrations

IdentityDbContext如何運行遷移非常相似,但是似乎沒有什么可以解決我的問題。 我已經完成了研究,關於在遷移中使用IdentityDbContext信息似乎並不多。

一些背景知識...我繼承了一個使用Entity Framework的Web應用程序(我不太熟悉)。 我不確定packages.config缺少哪些庫。 我最終得到了排序,發現它使用了實體框架。 我目前已將其設置為使用6.2.0,並且該應用似乎正常運行。

最初的開發人員已經實現了IdentityDbContext並且正在使用自動遷移。 他還已經定義了一個DbMigrationsConfiguration實現,我在其中添加了一些種子查詢。

到目前為止,我一直在使用自動遷移,但是我知道不建議將其用於生產。 而且我有幾次不得不丟失數據才能部署該應用程序,所以這可能就是為什么我不應該使用它的原因。

這使我開始使用現有數據庫進行代碼優先遷移 ,特別是為現有模式設置遷移。 那就是我迷路的地方。 這些指令似乎都不適合IdentityDbContext 這是我嘗試過的:

PM> Enable-Migrations
No context type was found in the assembly 'WebApp'.

PM> Enable-Migrations -ProjectName DataLayer
No context type was found in the assembly 'DataLayer'.

因此,這使我開始質疑IdentityDbContext是否還可以工作。 因此,接下來,我嘗試將上下文更改為繼承自DbContext ,這也意味着必須為用戶和角色添加DbSet屬性,以便我的代碼可以編譯(盡管我從未測試過該應用程序是否仍然有效)。 現在,這是我嘗試的方法:

PM> Enable-Migrations -ProjectName DataLayer
Migrations have already been enabled in project 'DataLayer'. To overwrite 
the existing migrations configuration, use the -Force parameter.

PM> Add-Migration InitialCreate –IgnoreChanges -ProjectName DataLayer
No migrations configuration type was found in the assembly 'DataLayer'. (In 
Visual Studio you can use the Enable-Migrations command from Package Manager 
Console to add a migrations configuration).

我讀對了嗎? 它可以找到遷移配置,但是找不到嗎? 有人測試過這些工具嗎?

好的,因此我決定接受使用-Force的建議:

PM> Enable-Migrations -ProjectName DataLayer -Force
Code First Migrations enabled for project DataLayer.

PM> Add-Migration InitialCreate –IgnoreChanges -ProjectName DataLayer
The project 'WebApp' failed to build.

而且,正如它所說,該項目無法構建,因為我現有的DbMigrationsConfiguration實現被覆蓋。 我現有的實現有一個構造函數,該構造函數為種子查詢獲取了一些數據。 因此,僅出於測試目的,我決定不擔心那些種子查詢,因此保留了自動生成的配置並更新了我的應用程序代碼以進行補償。 現在開始創建初始遷移:

PM> Add-Migration InitialCreate –IgnoreChanges -ProjectName DataLayer
System.IO.FileNotFoundException: Could not load file or assembly 
'Microsoft.AspNet.Identity.Core, Version=2.0.0.0, Culture=neutral, 
PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system 
cannot find the file specified.
File name: 'Microsoft.AspNet.Identity.Core, Version=2.0.0.0, 
Culture=neutral, PublicKeyToken=31bf3856ad364e35'

因此,我已經通過NuGet將Microsoft.AspNet.Identity.Core 2.2.1安裝到了DataLayer ,但是顯然沒有在DataLayer項目中將其添加為參考。 添加了它,讓我們再試一次:

PM> Add-Migration InitialCreate –IgnoreChanges -ProjectName DataLayer
System.Data.Entity.ModelConfiguration.ModelValidationException: One or more 
validation errors were detected during model generation:

DataLayer.IdentityUserLogin: : EntityType 'IdentityUserLogin' has no key 
defined. Define the key for this EntityType.
DataLayer.IdentityUserRole: : EntityType 'IdentityUserRole' has no key 
defined. Define the key for this EntityType.
IdentityUserLogins: EntityType: EntitySet 'IdentityUserLogins' is based on 
type 'IdentityUserLogin' that has no keys defined.
IdentityUserRoles: EntityType: EntitySet 'IdentityUserRoles' is based on 
type 'IdentityUserRole' that has no keys defined.

我認為這些錯誤與我將IdentityDbContext切換到DbContext

我真的不知道從這里去哪里,我有點沮喪。 看來這似乎行不通。 如果確實必須在每次添加遷移時都必須將IdentityDbContext更改為DbContext ,那是不可接受的。

即使我確實做到了這一點,但在編寫直接的SQL DDL腳本很容易時,卻似乎有些費解。 我想這樣做的好處是遷移可以涵蓋多個版本的升級,但是可以使用DDL腳本和版本號表來處理,因此這也不是真正的問題。

我應該只是放棄使用遷移嗎? 還是完全是實體框架?

編輯:

我發現在程序集EF6中未找到任何上下文類型,這解釋了為什么IdentityDbContext無法正常工作。 顯然,沒有添加Microsoft.AspNet.Identity.Core作為參考,這似乎很奇怪,因為我有點希望將參考與NuGet一起添加。 但是,這僅解決了Enable-Migrations問題。 在Add-Migration上仍然沒有遷移配置錯誤。

好吧,我想我已經解決了所有問題。 因此,在Enable-Migrations工具的頂部顯然會吞下丟失的引用,而不是告訴您有關引用的信息,Add-Migration工具顯然需要-ConfigurationTypeName才能找到配置。 尚不完全清楚為什么,因為Enable-Migrations能夠在沒有該配置的情況下找到配置。 但是只要有效,這就是我所關心的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM