2014-04-09 40 views
1

我寫C#/ ASP.NET MVC5 web應用程序,和我打下面的錯誤,當我嘗試做一個新的遷移「定義此EntityType的密鑰。」當包括類型ApplicationUser的屬性

ToTheMoon.DAL.IdentityUserLogin: : EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType. 
ToTheMoon.DAL.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. 

我已經包含了一個屬性( ?)在我的一個型號中是ApplicationUser。我認爲,這是什麼原因引起的:

public ApplicationUser Requester { get; set; } 

我非常清楚的事實是,在一般我應該明確標記的東西作爲與[Key]註釋的關鍵,但我不相信這也適用錯誤消息描述了屬於MVC5「包含電池」登錄系統的類。

我已經看到了一些關於這是由於錯誤地覆蓋一個方法(現在不記得它現在是什麼)而引起的,但我沒有這樣做,所以我在網上發現的所有其他問題都沒用我。

任何感激。

編輯:這是我的背景,它是相當裸露的。

public class ApplicationDbContext : IdentityDbContext<ApplicationUser> 
{ 
    public ApplicationDbContext() : base("DefaultConnection") { } 
} 
+0

你可以顯示你的上下文OnModelCreating?如果您不調用base.OnModelCreating,可能會發生這種情況,因爲它不再爲身份實體設置配置。 – Dismissile

+0

@Dismissile我沒有'OnModelCreating'覆蓋,這是必要的嗎?對不起,新框架。 –

+0

這不是必需的,所以這可能不是你的問題。但是,您可以將代碼發佈到您的上下文中嗎? – Dismissile

回答

0

從問題的意見:

看來,加入ApplicationUser請求者屬性在不同的上下文中定義的實體。因爲它在不同的上下文中,上下文對所有Identity實體的配置一無所知,因爲它是由IdentityDbContext.OnModelCreating方法設置的。

相關問題