我在更新父实体时一直在尝试更新子实体。但这是行不通的。这是我的父母(寄售)模型。
public class Consignment
{
public Guid Id { get; set; }
public DateTime? DateCreated { get; set; }
public DateTime? DateModified { get; set; }
public Guid AccountId { get; set; }
...
...
public Guid BookedByUser { get; set; }
public Guid? ModifiedByUser { get; set; }
public string DescriptionToLeave { get; set; }
public Account account{ get; set; }
public User bookedbyUserNavigation { get; set; }
public User quotedbyUserNavigation { get; set; }
public ICollection<ConsignmentLine> consignmentLine { get; set; }
public ICollection<Legging> leg { get; set; }
}
子实体是consignmentLine和Legging。它们如下:
public class ConsignmentLine
{
public Guid Id { get; set; }
public string ItemName { get; set; }
...
public bool? Deleted { get; set; }
public Guid? ConsignmentId { get; set; }
public Guid? ItemId { get; set; }
public Guid? CommodityId { get; set; }
public string CommodityName { get; set; }
您可以看到consignmentLine具有父实体Consignment和ItemCommodity。绑腿是:
public class Legging
{
public Guid Id { get; set; }
public DateTime? DateCreated { get; set; }
...
public string ToLeg { get; set; }
public Guid? CarrierAccount { get; set; }
public Guid? ConsignmentId { get; set; }
public string CarrierName { get; set; }
}
我已经在我的PUT寄售控制器中编写了这段代码。
[HttpPut]
public async Task<IActionResult> PutConsignment(Consignment consignment)
{
var existingcon = _context.Consignment
.Include(cl => cl.ConsignmentLine)
.Include(l => l.Legging)
.Include(sc => sc.SenderContactNavigation)
.Include(rc => rc.ReceiverContactNavigation)
.Include(sa => sa.SenderAddressNavigation)
.Include(ra => ra.ReceiverAddressNavigation)
.Include(bu => bu.BookedByUserNavigation)
.Include(cu => cu.CheckedByUserNavigation)
.SingleOrDefault(c => c.Id == consignment.Id);
if (existingcon.Id != consignment.Id)
{
return BadRequest();
}
//update consignment
var newcon = _context.Entry(existingcon);
newcon.CurrentValues.SetValues(consignment);
//update or add consignmentLine
foreach (var conline in consignment.ConsignmentLine)
{
var line = existingcon.ConsignmentLine
.Where(x => x.Id == conline.Id)
.SingleOrDefault();
if (line != null)
{
var l = _context.Entry(line);
l.CurrentValues.SetValues(line);
}
else
existingcon.ConsignmentLine.Add(conline);
}
//remove consignmentline
foreach (var line in existingcon.ConsignmentLine.Where(x=> x.Id !=null).ToList())
{
if(!consignment.ConsignmentLine.Any(x=> x.Id == line.Id))
{
_context.ConsignmentLine.Remove(line);
}
}
//add or update Legging
foreach (var newleg in consignment.Legging)
{
var leg = existingcon.Legging
.Where(x => x.Id == newleg.Id)
.SingleOrDefault();
if (leg != null)
{
var le = _context.Entry(leg);
le.CurrentValues.SetValues(leg);
}
else
existingcon.Legging.Add(leg);
}
//remove legs
foreach (var leg in existingcon.Legging.Where(x=> x.Id !=null).ToList())
{
if(!consignment.Legging.Any(x=> x.Id == leg.Id))
{
_context.Legging.Remove(leg);
}
}
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!ConsignmentExists(consignment.Id))
{
return NotFound();
}
else
{
throw;
}
}
return Ok(consignment);
}
[当我尝试更新现有托运货物时,它在绑腿时抛出Null异常,但对consignmentLine来说效果很好。知道为什么吗?
实际上,代码将是:
[HttpPut]
public async Task<IActionResult> PutConsignment(Consignment consignment)
{
var existingcon = _context.Consignment
.Include(cl => cl.ConsignmentLine)
.Include(l => l.Legging)
.Include(sc => sc.SenderContactNavigation)
.Include(rc => rc.ReceiverContactNavigation)
.Include(sa => sa.SenderAddressNavigation)
.Include(ra => ra.ReceiverAddressNavigation)
.Include(bu => bu.BookedByUserNavigation)
.Include(cu => cu.CheckedByUserNavigation)
.SingleOrDefault(c => c.Id == consignment.Id);
if (existingcon.Id != consignment.Id)
{
return BadRequest();
}
//update consignment
var newcon = _context.Entry(existingcon);
newcon.CurrentValues.SetValues(consignment);
//update or add consignmentLine
foreach (var conline in consignment.ConsignmentLine)
{
var line = existingcon.ConsignmentLine
.Where(x => x.Id == conline.Id)
.SingleOrDefault();
if (line != null)
{
var l = _context.Entry(line);
l.CurrentValues.SetValues(line);
}
else
existingcon.ConsignmentLine.Add(conline);
}
//remove consignmentline
foreach (var line in existingcon.ConsignmentLine.Where(x=> x.Id !=null).ToList())
{
if(!consignment.ConsignmentLine.Any(x=> x.Id == line.Id))
{
_context.ConsignmentLine.Remove(line);
}
}
//add or update Legging
foreach (var newleg in consignment.Legging)
{
var leg = existingcon.Legging
.Where(x => x.Id == newleg.Id)
.SingleOrDefault();
if (leg != null)
{
var le = _context.Entry(leg);
le.CurrentValues.SetValues(leg);
}
else
existingcon.Legging.Add(newleg);
}
//remove legs
foreach (var leg in existingcon.Legging.Where(x=> x.Id !=null).ToList())
{
if(!consignment.Legging.Any(x=> x.Id == leg.Id))
{
_context.Legging.Remove(leg);
}
}
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!ConsignmentExists(consignment.Id))
{
return NotFound();
}
else
{
throw;
}
}
return Ok(consignment);
}