I have a classic Order item in my database:
public partial class ORDERS
{
public ORDERS()
{
this.ORDER_DETAIL = new HashSet<ORDER_DETAIL>();
}
public int ORDER_IDE { get; set; }
public string ORDER_STATE { get; set; }
public decimal ORDER_TOTAL { get; set; }
public decimal ORDER_TAXES { get; set; }
public decimal ORDER_SHIPPING_COST { get; set; }
public decimal ORDER_HANDLING_COST { get; set; }
public Nullable<System.DateTime> ORDER_SHIPPING_DATE { get; set; }
public string ORDER_BILLING_NAME { get; set; }
public string ORDER_BILLING_ADDRESS { get; set; }
public string ORDER_BILLING_CITY { get; set; }
public string ORDER_BILLING_REGION { get; set; }
public string ORDER_BILLING_COUNTRY { get; set; }
public string ORDER_BILLING_POSTAL_CODE { get; set; }
public string ORDER_SHIPPING_NAME { get; set; }
public string ORDER_SHIPPING_ADDRESS { get; set; }
public string ORDER_SHIPPING_CITY { get; set; }
public string ORDER_SHIPPING_REGION { get; set; }
public string ORDER_SHIPPING_COUNTRY { get; set; }
public string ORDER_SHIPPING_POSTAL_CODE { get; set; }
public string ORDER_COMMENT { get; set; }
public decimal ORDER_DETAIL_AMOUNT { get; set; }
public string ORDER_DESCRIPTION { get; set; }
public decimal ORDER_DISCOUNT { get; set; }
public virtual ICollection<ORDER_DETAIL> ORDER_DETAIL { get; set; }
}
As you can see, this items has a collection of ORDER_DETAIL
. In my project I want to save the modifications made to the order and keep only the current order details . So I am doing this:
public void SaveOrderModifications(ORDERS _orderToReceive)
{
using (mDb = new DatabaseEntity())
{
mDb.Database.Connection.Open();
var orderQry = from o in mDb.ORDERS
where o.ORDER_IDE == _orderToReceive.mOrderID
select o;
ORDERS originalOrder = orderQry.FirstOrDefault();
if (originalOrder == null)
{
throw new Exception("Invalid operation");
}
mDb.Entry(originalOrder).CurrentValues.SetValues(_orderToReceive);
mDb.SaveChanges();
}
}
So if my original order had 3 items, and my new order has 8, and from this order 2 of the original order were dumped, what do I need to do to effectively only keep the 8 new items? Do I need to iterate through all of them to see which ones are there, and which one aren't there anymore?
EDIT
I have found a solution, which is not elegant and consumes a bit of process:
foreach (var orderDetail in originalOrder.ORDER_DETAIL.ToList())
{
mDb.ORDER_DETAIL.Remove(orderDetail);
mDb.SaveChanges();
}
foreach (var orderDetail in orderToSave.ORDER_DETAIL)
{
mDb.ORDER_DETAIL.Add(orderDetail);
mDb.SaveChanges();
}
it implies that I flush all the older ORDER_DETAIL object before adding the new one, but I'm still looking for a more elegant / better way of doing things.
Typically I do it the same way you are doing it, but I check to see if the item is on the new one and only add and remove the changed items. It adds some elegance because you can use a Linq expression.
Something to the effect of:
foreach (var orderDetail in originalOrder.ORDER_DETAIL.Where(d => !newOrder.ORDER_DETAIL.Contains(d)).ToList())
{
mDb.ORDER_DETAIL.Remove(orderDetail);
mDb.SaveChanges();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.