我在這裏所描述的確切的問題一式兩份表格的結果: Register custom UserProfile in ASP.NET MVC4 results in duplicate tables當我註冊一個用戶與CodeFirst EF6 MVC5 Asp.NET,
但解決方案並不爲我工作,因爲它是MVC4和我使用MVC5。
問題是,當我嘗試註冊一個新用戶,複製數據庫中幾乎所有表的用於多個名稱,例如:圖片(複製到)圖片
編輯:
我有一個從IdentityUser擴展類,命名爲ApplicationUser,在這個模型把額外的屬性,比如郵政編碼,照片和名字的ID:
public class ApplicationUser : IdentityUser
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
public string Name { get; set; }
public string Photo { get; set; }
public int PostalCodeID { get; set; }
[ForeignKey("PostalCodeID")]
public virtual PostalCode PostalCode { get; set; }
public virtual ICollection<Auction> Auctions { get; set; }
public virtual ICollection<Bid> Bids { get; set; }
public virtual ICollection<Message> Messages { get; set; }
public virtual ICollection<Friend> Friends { get; set; }
public virtual ICollection<BlockHistory> Blocks { get; set; }
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser> {
public DbSet<PostalCode> PostalCode { get; set; }
public ApplicationDbContext()
// ConnectionString
: base("AuctionsContext", throwIfV1Schema: false)
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
}
PostalCodeID是表/型號「PostalCode」的外鍵。一個用戶可以有一個郵政編碼,但一個郵政編碼可以有零個或幾個用戶。
這裏是典型的「郵編」:
public class PostalCode {
public int ID { get; set; }
[RegularExpression(@"\d{4}-\d{3}", ErrorMessageResourceType=typeof(Resources), ErrorMessageResourceName="PostalCodeFormat")]
[Required(ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = "Required")]
[StringLength(8, ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = "Length")]
[Display(Name = "Name_PostalCode", ResourceType = typeof(Resources))]
public string ZipCode { get; set; }
public int LocalityID { get; set; }
public virtual Locality Locality { get; set; }
}
我啓用了遷移,但不能在自動模式下,我認爲這是在configuration.cs因爲發生的事情,我有這樣的代碼
public Configuration() {
AutomaticMigrationsEnabled = false;
}
當我創建第一個遷移,並更新數據庫,所有表都正確創建(用自己奇異的名字),除ASPNET個XXXX表,當第一個用戶註冊時被自動創建的,那就是當大多數表是重複的,而weblogger給出了這樣的錯誤:
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_dbo.AspNetUsers_dbo.PostalCodes_PostalCodeID". The conflict occurred in database "PSIProject", table "dbo.PostalCodes", column 'ID'.
The statement has been terminated.
System.Data.SqlClient.SqlException: The INSERT statement conflicted with the FOREIGN KEY constraint "FK_dbo.AspNetUsers_dbo.PostalCodes_PostalCodeID". The conflict occurred in database "PSIProject", table "dbo.PostalCodes", column 'ID'.
The statement has been terminated.
Linha 164: PostalCodeID = model.PostalCodeID
Linha 165: };
Linha 166: var result = await UserManager.CreateAsync(user, model.Password);
Linha 167: if (result.Succeeded)
Linha 168: {
我明白髮生了什麼導致這個錯誤,在某些方面,該IdentityUser找不到POSTALCODE表,因此,它會創建PostalCodes表, 和它的這個表,其中它的創建PostalCodes之間的關係和AspNetUsers(我檢查,並沒有任何關係連接表AspNetUsers和POSTALCODE)
PS: 我試圖把表(「郵編」)前公共類POSTALCODE,但是當註冊用戶時,我得到一個錯誤說dbo.AspNetUsers是一個無效的對象,所以我對數據庫進行了驗證,發現它並未創建這些自動錶AspNetxxxx表。
我很久以前面臨類似的錯誤,我真的不記得這個場景,但是我在設置外鍵的時候犯了一些錯誤,導致了重複的表格。而對於表名的複數化它的實體框架的默認行爲,但你總是可以阻止它做 – 2014-12-19 13:47:13