简体   繁体   中英

Entity Framework 6 unable to update column in DB

I am trying to attach a child entity (SofiePurchaseOrder code first) to the parent (sofie_dfill code first from existing DB table). I mark the entities as modified but after I save changes and check the DB, it hasn't changed.

public class dfill //yay legacy 
{
    public int id { get; set; }

    //other fields

    public Guid? PurchaseOrderID { get; set; }

    public virtual PurchaseOrder PurchaseOrder { get; set; }
}

public class PurchaseOrder 
{
    public Guid ID { get; set; }
    //other fields

    public virtual ICollection<sofie_dfill> DfillsOnThisOrder { get; set; }
}

I have a static class trying to update the PurchaseOrderID

    private static void MakeApiCallsForStore(int ShipToStoreID, List<dfill> dfills)
    {
        do
        {
            var ThisOrderID = Guid.NewGuid();
            List<dfill> AllDfillsForThisOrder = db.//grab all dfills with the same order_no


            var OrderLog = new PurchaseOrder();
            OrderLog.ID = ThisOrderID;
            OrderLog.BillToStoreID = LogisticsStoreID;
            OrderLog.ShipToStoreID = ShipToStoreID;
            OrderLog.EstimatedArrivalDate = DateTime.Now;
            db.PurchaseOrderLogs.Add(OrderLog);


            foreach (var dfill in AllDfillsForThisOrder)
            {
                dfill.PurchaseOrderID = ThisOrderID; //field looking to update

                db.ProductsOrdered.Attach(dfill);
                db.Entry(dfill).State = System.Data.Entity.EntityState.Modified;

            }



        } while ([condition]); 
        db.SaveChanges();
    }

I have tried multiple solutions including marking the individual field as modified. Adding the OrderLog works fine. Any assistance will be much appreciated!

Did you try adding the dfills to the Order PurchaseOrder's DfillsOnThisOrder's collection? You could potentially avoid the foreach

private static void MakeApiCallsForStore(int ShipToStoreID, List<dfill> dfills)
    {
        do
        {
        var ThisOrderID = Guid.NewGuid();
        List<dfill> AllDfillsForThisOrder = db.//grab all dfills with the same order_no


        var OrderLog = new PurchaseOrder();
        OrderLog.ID = ThisOrderID;
        OrderLog.BillToStoreID = LogisticsStoreID;
        OrderLog.ShipToStoreID = ShipToStoreID;
        OrderLog.EstimatedArrivalDate = DateTime.Now;
        OrderLog.DfillsOnThisOrder = new List<dfill>();
        OrderLog.DfillsOnThisOrder.AddRange(AllDfillsForThisOrder);
        db.PurchaseOrderLogs.Add(OrderLog);

    } while ([condition]); 
    db.SaveChanges();
}

Not sure if this will specifically solve your problem but it's explicitly setting up the relation in Entity Framework's object graph as the dfills being part of the related collection. Since you aren't saving the Purchase Order in the context before setting up the relation of the foreign key on the dfills, EF might be tripping over itself with the approach you're currently doing.

I do not see the relationship from PurchaseOrder to dfill , althought there is one from dfill to PurchaseOrder .

Anyhow, could you try with assigning OrderLog to dfill.PurchaseOrder ?

foreach (var dfill in AllDfillsForThisOrder)
{
    dfill.PurchaseOrder = OrderLog;
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM