如何设计最佳紧急联系模式以使用会员资格?

时间:2016-03-22 00:56:20

标签: c# asp.net asp.net-mvc ef-migrations

我必须将一些紧急信息存储到成员中,我在如何设计SQL表方面遇到了困难。我找到了这个very useful old question,但我仍在努力解决问题。

我注意到紧急联系人将获得与标准用户相同的信息。然后我想重用与我已经拥有的相同的结构。 让我告诉你我到目前为止(我首先使用代码,C#和我简化模型用于演示目的):

会员资格表

public class Membership
{
    public string Id { get; set; }
    public tring PhoneNumber { get; set; }
    public tring UserName { get; set; }
    public tring Email { get; set; }
    public bool IsDeactivated { get; set; }
    public bool IsAccountClosed { get; set; }
    /// <summary>
    /// In some cases like teammate, a membership can be linked to another membership. We'll be able to build teammate and other scenario.
    /// </summary>
    public string MembershipGroupId { get; set; }
    public int DisplayOrder { get; set; }
    public virtual UserProfile UserProfile { get; set; }
    /// <summary>
    /// The most of the time, a membership will have some emergency contacts
    /// </summary>
    public virtual ICollection<EmergencyContact> EmergencyContacts { get; set; }
}
}

如果紧急联系人可以链接到许多会员,并且会员可以有许多紧急联系,那么我应该建立多对多的关系。

EmergencyContact

public class EmergencyContact : Entity
    {
        public string MembershipId { get; set; }
        public string MembershipEmergencyId { get; set; }
        public virtual Membership Membership { get; set; }
        public virtual Membership MembershipEmergency { get; set; }
    }

我在下面定义了FluentAPI:

modelBuilder.Entity<EmergencyContact>()
                .HasKey(x => new { x.MembershipId, x.MembershipEmergencyId });


// EmergencyMembership to Membership
            modelBuilder.Entity<EmergencyContact>()
                .HasRequired(x => x.Membership)
                .WithMany(x => x.EmergencyContacts)
                .HasForeignKey(x => x.MembershipId);

            // EmergencyMembership to MembershipEmergencyId
            modelBuilder.Entity<EmergencyContact>()
                .HasRequired(x => x.MembershipEmergency)
                .WithMany(x => x.EmergencyContacts)
                .HasForeignKey(x => x.MembershipEmergencyId);

最后,我收到了这个错误 指定的架构无效。错误:未加载“EmergencyContact_Membership”关系,因为“会员资格”类型不可用。

正如您所看到的,EmergencyContact指的是两次会员资格,因为我希望紧急联系人会像其他任何用户一样拥有标准会员资格。 FluentAPI接缝不喜欢,我需要一些帮助才能使它工作。

非常感谢,

大卫

1 个答案:

答案 0 :(得分:0)

经过多次搜索,我得到了答案!我想做的事情被称为“自我引用多对多”。 在我的应用程序中,我希望使用与我已有的相同结构(Membership和UserProfile表)来存储紧急联系人关系。 为了做到这一点,如果你看一下我的问题,那么EmergencyContact表是好的,而且在Membership表中也是ICollection。

关键是Fluent API。定义多个自引用的方式是这样的:

modelBuilder.Entity<EmergencyContact>()
                .HasKey(x => new { x.MembershipId, x.MembershipEmergencyId });

//
            // For intance, David's membership can have a Joseph, Andrée and René membership as emergency contacts 
            // and those one can also be used as emergency contacts by Adeline's membership.
            //
            // A membership can have many emergencycontact
            modelBuilder.Entity<Membership>()
                .HasMany(x => x.EmergencyContacts)
                .WithRequired(x => x.Membership)
                .HasForeignKey(x => x.MembershipId)
                .WillCascadeOnDelete();

            // An emergencycontact can have many membership.
            modelBuilder.Entity<EmergencyContact>()
                .HasRequired(x => x.MembershipEmergency)
                .WithMany()
                .HasForeignKey(x => x.MembershipEmergencyId)
                .WillCascadeOnDelete(false);

希望这可以帮助别人,

大卫