因爲我已經創建了一個模式叫公司,包括公司基本信息&還銷售代表從不同的業務合作伙伴名單我小的web應用程序項目。這是我的聯繫型號:列表項沒有傳遞到視圖
public class Company
{
public int ID { get; set; }
public string Name { get; set; }
public string Promo { get; set; } // Yes or No field
public List<Contact> Contacts { get; set; }
public class Contact
{
[Key]
public int ContactID { get; set; }
public int CompanyID { get; set; }
public string ContactName { get; set; }
public string ContactNumber { get; set; }
}
}
這是我如何將數據傳遞到我的本地數據庫:
var companiesData = new Company[]
{
new Company
{
Name = "Some name", Promo="Y", Contacts = new List<Contact> { new Contact {ContactName="John Doe", ContactNumber="(828)292-2912", CompanyID=1},
}}, // ... some more contacts
};
foreach (Company c in companiesData)
{
context.Companies.Add(c);
}
context.SaveChanges();
如何加載聯繫人列表中的項目到剃刀看法?我在看我的數據庫,它顯示「聯繫人」的空白字段。其餘的數據顯示出來很好。
您節省看起來不正確,'context.Companies.Add(C)'應該是'CON text.Contacts.Add(C)'。而你需要先保存新的'Company'讓你有一個'CompanyID'保存與接觸 – user326608
'Contacts'屬性是一個'名單'所以它不是映射到現場,而是在'聯繫人的外鍵'與'公司'的'ID'匹配的表。你認爲它是什麼「空白領域」? –
granit
在SQL對象資源管理器的dbo.Companies表中。列聯繫人爲空白,其餘數據爲數據(從同一對象傳入) –