因为我已经创建了一个模式叫公司,包括公司基本信息&还销售代表从不同的业务合作伙伴名单我小的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表中。列联系人为空白,其余数据为数据(从同一对象传入) –