0

我正在使用带有默认 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); 

但我知道我需要以某种方式映射外键名称......有什么想法吗?

4

1 回答 1

0

您需要使用 WithRequiredPricipal() 而不是 WithOptional()

   modelBuilder.Entity<User>()
        .HasRequired(t => t.Aspnet_Membership)
        .WithRequiredPrincipal(t => t.User);
于 2013-02-11T21:02:08.413 回答