实体框架映射一对一Aspnet_Membership表

本文关键字:Membership Aspnet 一对一 框架 映射 实体 | 更新日期: 2023-09-27 17:58:39

我使用带有默认aspnet_Membership架构的EF5。

我有一个自定义的User对象,它有一个引用aspnet_Membership表的外键。

User对象中的外键称为"AspnetID"

我需要用流利的API映射这种一对一的关系,但我没有运气

对象如下所示:

public class aspnet_Membership
{
    [Key]
    public Guid UserId { get; set; }
    public string Email { get; set; }
    public DateTime CreateDate { get; set; }
    public aspnet_Users aspnet_User { get; set; }
    public User User { get; set; }
}
public class User
{
    public int UserID { get; set; }
    [MaxLength(50)]
    public string FirstName { get; set; }
    [MaxLength(50)]
    public string LastName { get; set; }
    public Guid AspnetID { get; set; } //This is the foreign key referencing aspnet_Membership
    public virtual aspnet_Membership Aspnet_Membership { get; set; }
}

我试过这个:

modelBuilder.Entity<User>().HasRequired(u => u.Aspnet_Membership).WithOptional(u => u.User); 

但我知道我需要映射外键名称的一些方式。。。有什么想法吗?

实体框架映射一对一Aspnet_Membership表

您需要使用WithRequiredPrincipal()而不是WithOption()

   modelBuilder.Entity<User>()
        .HasRequired(t => t.Aspnet_Membership)
        .WithRequiredPrincipal(t => t.User);