Save enity when the FK is unknown

Posts   
 
    
methodman
User
Posts: 194
Joined: 24-Aug-2009
# Posted on: 19-Aug-2010 09:50:13   

I want to insert a new entity and set its FK to the related master entity, which already exists.

I don't know the PK value of the master entity,but I know a Unique value of the master entity.

I tried this unsuccessfully


           var detailEntity= new detailEntity();

            detailEntity.Popis = "Default";
            detailEntity.masterEntity= new masterEntity{UniqueField= "XXX",IsNew = false};


and



           var masterEntity = new masterEntity();
            masterEntity.UniqueField= "XXX";

           masterEntity.IsNew = false;

            masterEntity.detailEntity.Add(new detailEntity{ Popis = "Default" });

The PK value gets set by a sequence.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 19-Aug-2010 10:19:42   

I know a Unique value of the master entity.

If you don't have the PK value of the master entity, then you have to fetch it first, before you associate the detail entity to it. And since you know the Unique value, you can fetch it using that value.