ASP.NET标识 - 如何映射一对一或零关系?

时间:2017-01-25 01:32:05

标签: c# asp.net-mvc entity-framework entity identity

如何使用Entity Framework Code First映射以下关系?

关系 - 作者是主体,ApplicationUser是依赖者:

enter image description here

ApplicationUser是IdentityUser的派生类。作者可以先创建,而不是用户,因此他们是关系的主体。每个用户都必须与作者相关联,但作者可能与没有ApplicationUser相关联。我尝试使用流畅的API和注释来做到这一点,但我不能说ApplicationUser的主键是Author的外键。非常感谢你!

型号:

 public class Author
{
    [Key]
    public string AuthorId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
    public List<string> AditionalEmails { get; set; }
    public virtual ICollection<PublicationData> Publication { get; set; }

    //User is optional.
    public virtual ApplicationUser User { get; set; }
}



    public partial class ApplicationUser : IdentityUser
{

    public string FirstName { get; set; }
    public string LastName { get; set; }

    public string Affiliation { get; set; }
    public string Position { get; set; }
    public DateTime CreatedOn { get; set; }
    public bool Deleted { get; set; }
    public int CountryCode { get; set; }
    public virtual Country Country { get; set; }
    public virtual ICollection<PublicationData> Publications { get; set; }
    public virtual ICollection<Request> Requests { get; set; }

    //Author is required
    public virtual Author Author { get; set; }
}

0 个答案:

没有答案