Forum:  LLBLGen Pro Runtime Framework

Thread:  May I assign entity value to new record


nopchan (User)   Posted on: 16-Jun-2008 12:34:26.

adapter = new DataModel.DatabaseSpecific.DataAccessAdapter(SetConnectionString);
RemoteAdapter= new DataModel.DatabaseSpecific.DataAccessAdapter(SetRemoteServerConnectionString);


NewApnameEntity = new ApnameEntity(ShowGljourEntity.ApnameCode.ToString().Trim());
adapter.FetchEntity(NewApnameEntity);
if (NewApnameEntity.IsNew)
{
     ShowApnameEntity = new ApnameEntity(ShowGljourEntity.ApnameCode.ToString().Trim());
     RemoteAdapter.FetchEntity(ShowApnameEntity);
     if (!ShowApnameEntity.IsNew)
     {
         NewApnameEntity = ShowApnameEntity;
         adapter.SaveEntity(NewApnameEntity);

     }

}

I can not save NewApnameEntity to my database.

goose (User)   Posted on: 16-Jun-2008 16:54:59.
You must synchronize the entity yourself, i.e. you should copy the values of the entity manually (maybe overriding the Equals method) and then saving the entity with your local adapter. See other threads regarding "Copying Entities".

nopchan (User)   Posted on: 17-Jun-2008 03:56:23.
Code:

adapter = new DataModel.DatabaseSpecific.DataAccessAdapter(SetConnectionString);
RemoteAdapter= new DataModel.DatabaseSpecific.DataAccessAdapter(SetRemoteServerConnectionString);
private ApnameEntity ShowApnameEntity;
private ApnameEntity NewApnameEntity;

adapter.StartTransaction(IsolationLevel.Serializable, "AddData");

NewApnameEntity = new ApnameEntity(ShowGljourEntity.ApnameCode.ToString().Trim());
adapter.FetchEntity(NewApnameEntity);
if (NewApnameEntity.IsNew)
{
     ShowApnameEntity = new ApnameEntity(ShowGljourEntity.ApnameCode.ToString().Trim());
     RemoteAdapter.FetchEntity(ShowApnameEntity);
     if (!ShowApnameEntity.IsNew)
     {
         CopyFieldsInEntity(ShowApnameEntity, NewApnameEntity);
        
         adapter.SaveEntity(NewApnameEntity);
         adapter.Commit();
         ApnameEntity New2ApnameEntity = new ApnameEntity (ShowGljourEntity.ApnameCode.ToString().Trim());
         adapter.FetchEntity(New2ApnameEntity );
         if (New2ApnameEntity .IsNew)
         {
                 Console.Write("Not Found Data");

         }
            else
             {
                     Console.Write("Found Data");

            }
     }

}


private void CopyFieldsInEntity(EntityBase2 srcEntity, EntityBase2 destEntity)
        {
            if (!srcEntity.IsNew)
                destEntity.Fields = (IEntityFields2)((EntityFields2)srcEntity.Fields).Clone();

            if (srcEntity.IsNew || srcEntity.IsDirty)
            {
                foreach (EntityField field in (EntityFields2)srcEntity.Fields)
                {
                    if (field.IsChanged)
                        destEntity.SetNewFieldValue(field.FieldIndex, field.CurrentValue);
                }
            }
        }



I am not found data. I can not save data but code is not error.

Remark . run on LLBLGenPro 2.6 with Sqlserver 2005 std.


nopchan (User)   Posted on: 17-Jun-2008 04:40:32.
I change new code then it's work ok.

Code:

private void CopyFieldsInEntity(EntityBase2 srcEntity, EntityBase2 destEntity)
        {
            if (!srcEntity.IsNew)
            {
                
                foreach (EntityField2 field in (EntityFields2)srcEntity.Fields)
                {
                    if ( !field.IsReadOnly)
                        destEntity.SetNewFieldValue(field.FieldIndex, field.CurrentValue);
                }
            }
            if (srcEntity.IsNew || srcEntity.IsDirty)
            {
                foreach (EntityField2 field in (EntityFields2)srcEntity.Fields)
                {
                    if (field.IsChanged && !field.IsReadOnly)
                        destEntity.SetNewFieldValue(field.FieldIndex, field.CurrentValue);
                }
            }
        }