如果您使用過ASP.NET MVC 4,您會注意到Internet應用程序的默認設置是使用SimpleMembership提供程序,這一切都很好,精細。實體框架代碼優先問題(SimpleMembership UserProfile表格)
問題自帶的默認數據庫的一代,他們有一個POCO的UserProfile
像這樣定義的:
[Table("UserProfile")]
public class UserProfile
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public string UserName { get; set; }
}
..然後將這樣產生的:
using (var context = new UsersContext())
{
if (!context.Database.Exists())
{
// Create the SimpleMembership database without Entity Framework migration schema
((IObjectContextAdapter)context).ObjectContext.CreateDatabase();
}
}
這工作得很好,數據庫生成得很好,工作沒有問題。但是,如果我改變POCO這樣並刪除數據庫:
[Table("UserProfile")]
public class UserProfile
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public string EmailAddress { get; set; }
public string FirstName { get; set; }
public string Surname { get; set; }
public string Country { get; set; }
public string CompanyName { get; set; }
}
只有第2列生成,UserId
和EmailAddress
。它工作正常代碼明智(說話登錄/註冊),但顯然沒有我的其他用戶數據存儲。
我在這裏錯過了什麼嗎?當然,它應該基於整個對象生成數據庫。
看起來很奇怪。這是可重複的嗎?即清理並重建所有內容,重新檢查結果。你在看正確的Db嗎? –
@HenkHolterman不幸的是,在過去的兩天裏,我一直在爲自己瘋狂,我甚至試着用自己的名字來歸屬每一列,而他們根本就不在那裏。更糟糕的是'UserId'和'EmailAddress'(或者我重命名的)*都很好。 –
@HenkHolterman還應該值得注意的是,我試圖在一個全新的項目上更改這張表,就像一個測試一樣,它也有同樣的問題。我還沒有嘗試過使用實體框架獨立生成數據庫,也許我會做下一步,但寧願保持儘可能與默認的ASP.NET MVC4結構保持一致.. –