在EntityFramework中加入两个表

时间:2016-09-25 19:08:46

标签: mysql linq asp.net-web-api

我有两张桌子:

  • 公司
  • 与一对多关系(一家公司中的多个联系人)的联系人

联系人表已编辑

public class Contact
{
   public Guid Id { get; set; }

    public DateTime dateCreated { get; set; }

    public DateTime updated { get; set; }

    public Boolean hidden { get; set; }

    //Personal Data

    public string title { get; set; }

    public string jobTitle { get; set; }

    public string firstName { get; set; }

    public string lastName { get; set; }

    public string department { get; set; }

    public string email { get; set; }

    public string logoUrl { get; set; }

    public string personalRemarks { get; set; }

    //Telephone list

    public ICollection<Phone> Phones { get; set; }

    //Addresses

    public ICollection<Address> Addresses { get; set; }

    //Bank Data

    public ICollection<Bankdata> Bankdatas { get; set; }

    //Tags

    public ICollection<Tag> Tags { get; set; }
}

}

公司表已编辑

public class Organization
{
    public Guid Id { get; set; }

    public DateTime dateCreated { get; set; }

    public DateTime dateUpdated { get; set; }

    public Boolean hidden { get; set; }

    //Company Data

    public string organizationName { get; set; }

    public string taxId { get; set; }

    public string trades { get; set; }

    public string organizationType { get; set; }

    public string actionRadius { get; set; }

    public string organizationRemarks { get; set; }

    public string web { get; set; }

    //Contacts

    public ICollection<Contact> Contacts { get; set; }

    //Tags

    public ICollection<Tag> Tags { get; set; }

}

}

我的存储库中有一个方法可以选择一家公司的所有联系人

    public Organization GetOrganizationById(Guid Id)
    {
        return _context.Organizations
            .Include(c => c.Contacts)
            .Where(c => c.Id == Id)
            .FirstOrDefault();
    }

但是因为我拥有的只是公司ID,所以我需要在两个表之间建立连接以获得名称。类似的东西:

SELECT contacts,*, Organization.name
FROM contacts
INNER JOIN Organization ON Organization.id = Contacts.organization_id
WHERE Organization.id = id;

我尝试了以下但没有成功:

    public Organization GetOrganizationById(Guid Id)
    {
        return _context.Organizations
            .Include(o => o.organizationName)
            .Include(c => c.Contacts)
            .Where(c => c.Id == Id)
            .FirstOrDefault();
    }

欢迎任何帮助

1 个答案:

答案 0 :(得分:1)

正确的方法是在Organization类和Contact类之间创建导航属性。以下代码将让您了解需要执行的步骤:

public class Contact
{
    ...

    // Foreign key for Organization
    public Guid OrganizationId { get; set; }

    // Related Organization entity
    [ForeignKey("OrganizationId ")]
    public Organization Standard { get; set; }
}

public class Organization
{
    ...

    // List of related Contacts
    public virtual ICollection<Contact> Contacts { get; set; }

}

使用此代码创建迁移后,您将按如下方式实施方法:

public Organization GetOrganizationById(Guid Id)
{
    return _context.Organizations
        .Include(c => c.Contacts)
        .Where(c => c.Id == Id)
        .FirstOrDefault();
}