c#ef - 在一个公共表与其他三个或更多表之间创建一对多/一个映射的最佳方法

时间:2016-09-25 20:13:35

标签: c# entity-framework database-design

我正在尝试使用entity-framework 6.1代码为多种类型创建Contact的公共数据库表,例如PersonCompanyBranch等。我一直试图找出在ContactPersonCompanyBranch之间实现一对一映射的最佳方法,其中每个表都是条目只有一个Contact

我有四张桌子。

public class Contact
{
    public Contact()
    {
        People = new HashSet<Person>();
        Companies = new HashSet<Company>();
        Branches = new HashSet<Branch>();
    }
    public int ContactId { get; set; }

    public ICollection<Person> People { get; set; }
    public ICollection<Company> Companies { get; set; }
    public ICollection<Branch> Branches { get; set; }
}

public class Person
{
    public int PersonId { get; set; }

    public int ContactId { get; set; }
    public virtual Contact Contacts { get; set; }        
}

public class Company
{
    public Company()
    {
        Members = new HashSet<Member>();
    }
    public int CompanyId { get; set; }

    public int ContactId { get; set; }
    public virtual Contact Contacts { get; set; }     
}

public class Branch
{
    public int BranchId { get; set; }

    public int ContactId { get; set; }
    public virtual Contact Contacts { get; set; }
}

问题:在当前的实现中,我可以存储每个PersonCompanyBranch的多个联系人,即这些表与{{1}之间的多对多关系}}。相反,我想为每个ContactContactPerson存储唯一的一个Company,因为它应该只有一个。

我尝试了下面的实现,但在Branch

检索Contact信息时出错了
PersonId

检索public class Contact { public Contact() { People = new HashSet<Person>(); } public int ContactId { get; set; } public virtual Person People { get; set; } } public class Person { public int PersonId { get; set; } public virtual Contact Contacts { get; set; } } public class ContactMap : EntityTypeConfiguration<Contact> { public ContactMap() { // 1-1 relationships HasOptional(p => p.People).WithOptionalPrincipal(c => c.Contacts).Map(c => c.MapKey("ContactId")); } } 信息

时出错
Contact

_db.People.Join(_db.Contacts, p => p.ContactId, c => c.ContactId, (p, c) => new { p, c }) .Select(x => => new { x.p.PersonId, x.c.ContactId }) .OrderBy(pid => pid.Id) .ToList(); 它会抛出错误“无法解析符号'ContactId'”,因为p.ContactId实体/类中没有ContactId的定义。

任何建议都将受到高度赞赏。

我想有一个最终的输出/数据库结构如下:

enter image description here

1 个答案:

答案 0 :(得分:1)

我也遇到过同样的问题。在我看来,这里最好的方法是创建一个主表,比如Party。在此表与其他主表(PersonCompanyBranch)之间创建一对一关系,并创建 one-to-主表(Party)和Contact表之间的零或一个关系。

/// <summary>
/// Model for Party, which is general form of Persons and Companies
/// </summary>
public class Party
{
    public int PartyID { get; set; }

    // Navigation properties
    public virtual Company Company { get; set; }
    public virtual Person Person { get; set; }
    public virtual Contact Contact { get; set; }
}

public class Person
{
    public int PersonID { get; set; }
    // Other properties.....

    // Navigation properties
    public virtual Party Party { get; set; }
}

public class Company
{
    public int CompanyID { get; set; }
    // Other properties

    // Navigation properties
    public virtual Party Party { get; set; }
}

public class Contact
{
    public int ContactID { get; set; }
    // Other properties...

    // Navigation properties
    public virtual Party Party { get; set; }
}

This link帮助我创建了我的模型。