Answer the question
In order to leave comments, you need to log in
Entity Framework 6 complex model update - how?
Thanks in advance to everyone who answers my question. The bottom line is this - there is a very complex model that describes the application for the delivery of goods (we are doing an internal logistics service - Web API 2 + AngularJS + Angular Material + Google Maps).
The application contains one point of departure, one car and several cargoes. Each cargo in turn contains several destinations.
DB scheme
Question - how to implement editing of this request?
Thanks in advance for your replies!
Answer the question
In order to leave comments, you need to log in
Request handler for editing a delivery request
public async Task<IHttpActionResult> EditDeliveryBidAsync(long id, EditBid bid)
{
var dbBid = await DeliveryManager.GetBidAsync(id);
if (dbBid != null)
{
var oldCargoIds = dbBid.Cargos.Select(c => c.Id).ToArray();
Mapper.Map(bid, dbBid);
if (await DeliveryManager.UpdateBidAsync(dbBid, oldCargoIds))
return Ok();
}
return BadRequest();
}
public async Task<bool> UpdateBidAsync(DeliveryBid bid, long[] oldCargoIds)
{
DbContext.Entry(bid).State = EntityState.Modified;
await DbContext.DeliveryCargos.ForEachAsync(c =>
{
if (oldCargoIds.Any(o => o == c.Id))
DbContext.DeliveryCargos.Remove(c);
});
var updatedBid = await DbContext.SaveChangesAsync();
return updatedBid > 0;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question