我需要在实体框架中的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'键。这可能是问题吗? –