Ef复合键流利api与外键

时间:2017-11-20 14:27:50

标签: c# entity-framework

我有

public class FriendshipConfiguration: EntityTypeConfiguration<Friendship>
{
    public FriendshipConfiguration()
    {
        HasKey(k => new { k.FromUserId, k.ToUserId })
            .HasRequired(e => e.ApplicationUser)
            .WithMany(m=> m.Friendships)
            .HasForeignKey(e => new { e.FromUserId, e.ToUserId })
            .WillCascadeOnDelete(false);
    }
}

使用以下表格

public class Friendship
{
    public ApplicationUser ApplicationUser { get; set; }
    public string FromUserId { get; set; }
    public string ToUserId { get; set; }
    public FriendshipStatus FriendshipStatusId { get; set; }
    public DateTime RequestedDateTime { get; set; }
    public DateTime? ResponseDateTimeTime { get; set; }
}

public class ApplicationUser
{
    ...
    public ICollection<Friendship> Friendships { get; set; }
}

我希望最终拥有(FromUserId,ToUserId)主复合键以及ApplicationUser的外键。

我遇到以下错误

  

Friendship_ApplicationUser_Target_Friendship_ApplicationUser_Source ::关系约束中的从属角色和主要角色中的属性数必须相同。

0 个答案:

没有答案