Clone entity

Posts   
 
    
e106199
User
Posts: 175
Joined: 09-Sep-2006
# Posted on: 08-Jan-2011 06:06:12   

Hi, i am trying to create a new entity by cloning an old one and save the new entity back into db. it is my studenthistory entity and here is what i have done:

var latestHistory = new StudentHistoryEntity(studentId, termId); var newHistory = new StudentHistoryEntity { Fields = latestHistory.Fields.Clone() };

newHistory.TermId = newTermId; newHistory.Save();

I keep getting Cannot insert the value NULL into column 'StudentID', table 'StudentHistory'; column does not allow nulls. INSERT fails. The statement has been terminated.. Check InnerException, QueryExecuted and Parameters of this exception to examine the cause of this exception.

Am i missing something here?

ps: also tried setting the IsDirty property to true and also tried setting IsChanged property to true. None worked.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 09-Jan-2011 23:57:41   

For the PK, I think you will need to use this:

newHistory.Fields[0].ForcedCurrentValueWrite(studentId);

You also could use the serialization trick: http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=7568

David Elizondo | LLBLGen Support Team
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39908
Joined: 17-Aug-2003
# Posted on: 10-Jan-2011 11:33:49   

WHen using serialization, be aware that it's recursive (so serializes all related entities as well)

Frans Bouma | Lead developer LLBLGen Pro