Why can't I get this work...?

Posts   
 
    
Jeff M
User
Posts: 250
Joined: 04-Aug-2004
# Posted on: 02-Sep-2005 21:11:32   

Latest version LLBLGenPro Adapter

Here's the code:


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

Problem is plDetail.CatalogueDetail is ALWAYS null even though "new CatalogueDetailEntity(detail.Fields) is successful and returns an entity. Therefore when I step through, plDetail.CatalogueDetail.Manufacturer = "" even though detail.Manufacturer returns a value. plDetail.CatalogueDetail will not allow itself to be assigned.

What am I doing wrong?

Thanks.

Jeff

Jeff M
User
Posts: 250
Joined: 04-Aug-2004
# Posted on: 02-Sep-2005 23:15:50   

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.

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.

Sort of like the Heisenberg Uncertainty Principle, as any Physics major should know.

Can you help?

Thanks.

Jeff

Paul.Lewis
User
Posts: 147
Joined: 22-Aug-2005
# Posted on: 03-Sep-2005 04:12:07   

plDetail.CatalogueDetail = new CatalogueDetailEntity(detail.Fields);

If you look at plDetail.CatalogueDetail you should find that it is a collection type. I believe your problem stems from assigning an entity to a collection instead of adding it.

try


plDetail.CatalogueDetail.add(new CatalogueDetailEntity(detail.Fields));

Jeff M
User
Posts: 250
Joined: 04-Aug-2004
# Posted on: 03-Sep-2005 06:20:33   

Paul.Lewis wrote:


plDetail.CatalogueDetail = new CatalogueDetailEntity(detail.Fields);

If you look at plDetail.CatalogueDetail you should find that it is a collection type. I believe your problem stems from assigning an entity to a collection instead of adding it.

try


plDetail.CatalogueDetail.add(new CatalogueDetailEntity(detail.Fields));

No... CatalogueDetail is an entity, not a collection. Relationship is N:1.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 03-Sep-2005 08:19:22   

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.

Frans Bouma | Lead developer LLBLGen Pro