This question is locked. New answers and comments are not allowed.
I am running into a situation where I am crashing because of a null value it is trying to put in a FK field. I am doing similar inserts or adds in many other places and I can't figure out why in this instance the save is crashing. So I am wondering if the ORM is trying to insert the child item first or if it is not updating the PK/FK after it inserts the parent item?
Here is my code which is in a background worker
int index = 0;
foreach (var prod in pasteList.OrderBy(p => p.SortOrder))
{
string pcString = "Processing " + (index + 1).ToString() + " of " + pasteList.Count.ToString();
InvokeOnUIThread(() => { this.BusyMessage = pcString; });
index++;
FcoProduct p = new FcoProduct();
Utility.CopyObject.CopyValues(prod, p);
p.SortOrder = SelectedStyleGroup.FcoProducts.Count;
p.FcoStyleGroup = SelectedStyleGroup;
SelectedStyleGroup.FcoProducts.Add(p);
Dc.Add(p);
p.CtlgProduct = (from x in Dc.CtlgProducts
where x.ProductID == prod.CatalogProductID
select x).First();
foreach (var opt in prod.FcoOptions)
{
FcoOption newOption = new FcoOption();
Utility.CopyObject.CopyValues(opt, newOption);
newOption.FcoProduct = p;
newOption.ProductID = p.ProductID;
p.FcoOptions.Add(newOption);
Dc.Add(newOption);
newOption.CtlgOption = (from x in Dc.CtlgOptions
where x.OptionID == opt.CatalogOptionID
select x).First();
}
foreach (var sk in prod.FcoSketches)
{
FcoSketch newSk = new FcoSketch();
Utility.CopyObject.CopyValues(sk, newSk);
newSk.FcoProduct = p;
newSk.ProductID = p.ProductID;
p.FcoSketches.Add(newSk);
Dc.Add(newSk);
foreach (var sd in sk.FcoSketchDetails)
{
FcoSketchDetail sdNew = new FcoSketchDetail();
sdNew.FcoSketch = newSk;
Utility.CopyObject.CopyValues(sd, sdNew);
newSk.FcoSketchDetails.Add(sdNew);
Dc.Add(sdNew);
}
}
//Add a location for each quantity this item has
for (int i = 0; i < p.Quantity; i++)
{
FcoProductLocation pl = new FcoProductLocation();
pl.CheckPointID = 27;
pl.DoorsCompleted = false;
pl.FramesCompleted = false;
pl.ManuallyLocated = false;
pl.SortOrder = i;
pl.FcoProduct = p;
p.FcoProductLocations.Add(pl);
Dc.Add(pl);
}
Here is my code which is in a background worker
int index = 0;
foreach (var prod in pasteList.OrderBy(p => p.SortOrder))
{
string pcString = "Processing " + (index + 1).ToString() + " of " + pasteList.Count.ToString();
InvokeOnUIThread(() => { this.BusyMessage = pcString; });
index++;
FcoProduct p = new FcoProduct();
Utility.CopyObject.CopyValues(prod, p);
p.SortOrder = SelectedStyleGroup.FcoProducts.Count;
p.FcoStyleGroup = SelectedStyleGroup;
SelectedStyleGroup.FcoProducts.Add(p);
Dc.Add(p);
p.CtlgProduct = (from x in Dc.CtlgProducts
where x.ProductID == prod.CatalogProductID
select x).First();
foreach (var opt in prod.FcoOptions)
{
FcoOption newOption = new FcoOption();
Utility.CopyObject.CopyValues(opt, newOption);
newOption.FcoProduct = p;
newOption.ProductID = p.ProductID;
p.FcoOptions.Add(newOption);
Dc.Add(newOption);
newOption.CtlgOption = (from x in Dc.CtlgOptions
where x.OptionID == opt.CatalogOptionID
select x).First();
}
foreach (var sk in prod.FcoSketches)
{
FcoSketch newSk = new FcoSketch();
Utility.CopyObject.CopyValues(sk, newSk);
newSk.FcoProduct = p;
newSk.ProductID = p.ProductID;
p.FcoSketches.Add(newSk);
Dc.Add(newSk);
foreach (var sd in sk.FcoSketchDetails)
{
FcoSketchDetail sdNew = new FcoSketchDetail();
sdNew.FcoSketch = newSk;
Utility.CopyObject.CopyValues(sd, sdNew);
newSk.FcoSketchDetails.Add(sdNew);
Dc.Add(sdNew);
}
}
//Add a location for each quantity this item has
for (int i = 0; i < p.Quantity; i++)
{
FcoProductLocation pl = new FcoProductLocation();
pl.CheckPointID = 27;
pl.DoorsCompleted = false;
pl.FramesCompleted = false;
pl.ManuallyLocated = false;
pl.SortOrder = i;
pl.FcoProduct = p;
p.FcoProductLocations.Add(pl);
Dc.Add(pl);
}