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!