我有兩個表:加入兩個表中的EntityFramework
- 公司
- 的接觸,一個一對多的關係(在一個公司多次接觸)
Contacts表EDITED :
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; }
}
}
家公司表EDITED:
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();
}
任何幫助將受到歡迎
是否使用*實體框架代碼 - 第一*? –
是的,我正在遵循代碼優先方法。我創建了類,然後使用EntityFramework來生成我的數據庫。我的聯繫表確實有一個「organizationId」列。然而,我粘貼在這裏的代碼並沒有帶回這一列。而且這一點,我不知道如何添加一個「選擇」聲明,使公司的名稱 –
,但您的模型不反映您的查詢。根據你的查詢,你應該在'Contact'模型上有'OrganizationId'屬性。另外,爲了能夠使用'Include',你應該有[導航屬性](https://msdn.microsoft.com/en-us/data/jj713564.aspx),比如'public virual ICollection Contacts {get;組; }' –