我需要在實體框架中的ApplicationUser
和我自己的類之間的1:1關係。實體框架中的1:1關係
我這樣做:
public class ApplicationUser : IdentityUser
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
return userIdentity;
}
/*Realations*/
public virtual ICollection<Comment> Comments { get; set; }
public virtual Posts Post { get; set; }
}
public class Posts : System.Object
{
public Posts()
{
this.PostDate = DateTime.Now;
this.PostViews = 0;
this.PostPic = "d.jpg";
}
[Key]
public int PostID { get; set; }
public string PostName { get; set; }
public string PostSummery { get; set; }
public string PostDesc { get; set; }
public string PostPic { get; set; }
public DateTime PostDate { get; set; }
public int PostViews { get; set; }
public string postMetaKeys { get; set; }
public string PostMetaDesc { get; set; }
public string UserId { get; set; }
[Key, ForeignKey("UserId")]
public virtual ApplicationUser ApplicationUser { get; set; }
public int CategoryID { get; set; }
[Key, ForeignKey("CategoryID")]
public virtual Categories Category { get; set; }
public virtual ICollection<Comment> commnets {get; set;}
}
但我得到一個異常時,我寫的命令「添加遷移關係」裏面Nuget
控制檯。
無法確定 類型「FinalKaminet.Models.ApplicationUser」和「Models.Posts」之間的關聯的主要端。此關聯的主體端必須使用 顯式配置關係流暢API或數據註釋。
我還添加以下代碼內IdentityModels
,但被示出的另一錯誤:
ApplicationUser_Post_Target:模型生成過程中檢測到
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<ApplicationUser>()
.HasOptional(f => f.Post)
.WithRequired(s => s.ApplicationUser);
}
一個或多個驗證錯誤:多重性是不角色 'ApplicationUser_Post_Target'在'ApplicationUser_Post'關係中有效。 由於從屬角色屬性不是關鍵屬性,所以從屬角色的多重性的上界必須是'*'。
出了什麼問題?
顯示了什麼*另一個錯誤*? –
@AdilMammadov,已更新的問題。 – Farzaneh
我記得'IdentityUser'有'string'鍵。這可能是問題嗎? –