Saving existing entity as a new row in db

Posts   
 
    
agask2
User
Posts: 2
Joined: 17-Feb-2006
# Posted on: 17-Feb-2006 20:48:24   

Hi is there any way to take an exisiting entity pulled from the database, change a few values and insert it as a new row in the database. Kind of like a copy but with new Pkeys.

The only way i have found so far it to create a new entity and set all the values manually which kind of sucks when you have alot of columns.

Any Help appreciated.. Thanks

agask2
User
Posts: 2
Joined: 17-Feb-2006
# Posted on: 17-Feb-2006 22:22:43   

Clone an entity except for readonly varaibles.

public static IEntity2 Clone(IEntity2 entity) { IEntity2 cloneEntity = (IEntity2)Activator.CreateInstance (entity.GetType());

for(int i=0; i<entity.Fields.Count; ++i)
{                   
    if(!entity.Fields[i].IsReadOnly)
    {
        cloneEntity.Fields[i].CurrentValue = entity.Fields[i].CurrentValue;
    }
}

return accountEnum;

}

bclubb
User
Posts: 934
Joined: 12-Feb-2004
# Posted on: 18-Feb-2006 02:47:18   

Is your second post how you fixed this problem? should return accountEnum; be return cloneEntity;

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39930
Joined: 17-Aug-2003
# Posted on: 18-Feb-2006 09:40:09   

Clone an entity can be smpler: CustomerEntity c = new CustomerEntity(); c.Fields = (IEntityFields2)((EntityFields2)myCustomer.Fields).Clone();

You then have to set flags and values. In v2 I've planned to add a wrapper for this, so it's easier to clone an entity, as it's an often returning question.

Frans Bouma | Lead developer LLBLGen Pro