Jeff wrote:
More info...
private void AddCatalogueDetailToPriceListDetail(PriceListEntity entity, CatalogueDetailEntity detail)
{
PriceListDetailEntity plDetail = new PriceListDetailEntity();
plDetail.CatalogueDetail = new CatalogueDetailEntity(detail.Fields);
// plDetail.SetRelatedEntity(detail, "CatalogueDetail");
plDetail.CatalogueID = detail.CatalogueID;
plDetail.CatalogueDetail.Manufacturer = detail.Manufacturer;
plDetail.CatalogueDetail.Description = detail.Description;
plDetail.CatalogueDetail.SKU = detail.SKU;
plDetail.Applied = false;
entity.PriceListDetails.Add(plDetail);
}
First of all, I commented out the SetRelatedEntity line. It is not necessary. However, plDetail.CatalogueDetail is still null.
You shouldn't need to call SetRelatedEntity() really, but use the properties.
The problem seems to be the "plDetail.CatalogueID = detail.CatalogueID" line. When I set that variable, it forces plDetail.CatalogueDetail to null. CatalogueID is part of the PK for plDetail. Now, the problem seems to be that I can set plDetail.CatalogueID OR I can set plDetail.CatalogueDetail, but I can't seem to set both.
Don't set FK fields if you're also setting the object reference, as you don't need to do that, the framework will take care of that.
Doesn't it sync the plDetail.CatalogueID field automatically after:
plDetail.CatalogueDetail = new CatalogueDetailEntity(detail.Fields);
?
(it might be it doesn't, because you're setting it to a NEW entity. Try setting it to detail instead, OR first do:
CatalogueDetailEntity newCatalog = new CatalogueDetailEntity(detail.Fields);
newCatalog.IsNew = false;
and then:
plDetail.CatalogueDetail = new CatalogueDetailEntity(detail.Fields);
which will sync the FK field in plDetail.