1

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!

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much, this caused me to rethink the whole process. I was trying to do too much at once, separated out the pieces and it works fine now :)
happy to help! I'm glad you figured it out
1

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;
}

1 Comment

this would work too - i think setting only the foreign key by id before actually committing the new entity (the PurchaseOrder) to the database can cause issues

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.