Copy content to new entity and change PK

Posts   
 
    
jacob
User
Posts: 32
Joined: 20-May-2005
# Posted on: 19-Dec-2005 16:18:12   

Hey

I need to be able to load an entity, change some data and save the entity with a new PK. Using SelfServicing

Is their any method to do this quickly.

I tried setting the Isnew field to true, but that just erased all fields. (or the fields dont get saved because some property says it hasn't changed?)

What i have done instead is to loop trough every field set them on a new entity, change PK and Save the entity

My current code, which works:


      AdminSiteEntity entityToCopy = new AdminSiteEntity(40001);

      AdminSiteEntity entityNew = new AdminSiteEntity();

      foreach( IEntityFieldCore entityFieldCore in entityToCopy.Fields.GetAsEntityFieldCoreArray()){
        if (!entityFieldCore.IsNull)
          entityNew.SetNewFieldValue( entityFieldCore.FieldIndex, entityToCopy.GetCurrentFieldValue(entityFieldCore.FieldIndex));
      }

      entityNew.SiteGuid = 40004;
      entityNew.Save();

bclubb
User
Posts: 934
Joined: 12-Feb-2004
# Posted on: 20-Dec-2005 02:37:43   

Here's a post that gives you a little background on the issue and around the last post you will see a way to change the PK.

jacob
User
Posts: 32
Joined: 20-May-2005
# Posted on: 20-Dec-2005 08:38:40   

Think you forgot the url...

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 20-Dec-2005 08:51:36   

Let me try to find the post instead, just a sec. (edit) Brian might have meant this one: http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=2676

But I'm not sure, I'll ask him later today so he can update his post simple_smile

Frans Bouma | Lead developer LLBLGen Pro
jacob
User
Posts: 32
Joined: 20-May-2005
# Posted on: 20-Dec-2005 09:37:20   

Thanks for the link, my code is now changed (still just testcode) and it works.

Although i didn't find an enumerator for entity.Fields so made and for-loop and field.CurrentValue don't return null if the field is null, so changed that to check property field.IsNull instead.


      AdminSitesEntity entity= new AdminSitesEntity(40001);

      entity.IsNew = true;
      entity.Fields.State = EntityState.New;
      entity.IsDirty = true;
      for(int i = 0; i< entity.Fields.Count; i++){
        entity.Fields[i].IsChanged =( ! entity.Fields[i].IsNull  );
      }
      entity.SiteGuid = 40005;
      entity.Save();