我有一个称为quote的表,该表有很多服务,每个服务都有很多员工。
quote:
public int Id {get; set;}
pbulic ICollection<Services> services {get;set}
Services:
public int Id {get;set;}
public int QuoteId {get;set;}
[ForeignKey("QutoeId")]
public Quote quote {get;set;}
public ICollection<Staff> Staff {get;set;}
Staff:
public Int Id {get;set;}
public ServiceId {get;set;
[ForeignKey("StaffId")]
public Service service {get;set;}
public string StaffName {get;set;}
这不是我的真实数据库,因此请忽略任何错别字。(或不正确的编码礼节。
我有问题。我是否要将表保存到数据库。我有一个Quote对象,提供了许多服务和许多员工。
我是否必须遍历所有这些,才能将它们手动添加到数据库中。
我只是尝试:
dbContext.Quote.Add(quote);
dbContext.SaveChanges();
但是,这只会将Quote表保存到数据库中,而不会将子表保存到数据库中。我是否必须做一些愚蠢的事情:
foreach (var staff in Quote.Services.Staff)
{
dbContext.Staff.Add(staff)
}
foreach (var service in Quote.Services)
{
dbContext.Services.Add(services)
}
您只需将子表添加到dbcontext及其父表中。您可以尝试以下所述:
dbContext.Quote.Add(quote);
foreach (var service in Quote.Services)
{
quote.services .Add(service);
dbContext.Services.Add(service);
}
dbContext.SaveChanges();
同样,将人员对象添加到其父服务对象和dbcontext中
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句