save an existing entity to a new row

Posts   
 
    
Hooman
User
Posts: 9
Joined: 24-Aug-2007
# Posted on: 13-Feb-2008 12:52:21   

Hi,

I would like to add an existing entity as a new record without having to create the new entity object from scratch. I tried a code like below but i get some null value is not permitted to save type of exception. it seems like llbl send null for some values. can you please put me in right direction. nonetheless, I know a code likedisappointed :

foreach (IEntityField2 field in address.Fields) { field.IsChanged = true; }

will work. but I dont want to change every fields for such an operation.

Thankssunglasses , Hooman

TblAddressEntity address= new TblAddressEntity(addressId); try {

            if (adapter.FetchEntity(address) == false)
            {
                throw new ODOperationFailedException("the address id does not exists");
            }

            address.Fields.State = EntityState.New;             
            address.Line1 = line1;
            address.IsNew = true;


            if (adapter.SaveEntity(address, true) == false)
            {
                throw new ODOperationFailedException("the user cannot be saved!");
            }

        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
        return address.AddressId;
Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 13-Feb-2008 15:21:15   

I tried a code like below but i get some null value is not permitted to save type of exception

Would you please post the exact exception text?

When you save an entity, if there is an entity field that's not set to a value, the field is not generated in the INSERT / UPDARE statement, i.e. LLBLGen doesn't set null for unchanged fields. It simply ignores them.

So most probably you get this exception from the database, as you attempt to save an entity without setting values for fields which don't accept null in the database.

So either you have default values in the database to solve this issue, or you set these fields to some value before saving. And if you want to set default values automatically in the code rather than the database, you can have them set in Entity validation code (ValidateEntityBeforeSave). So that you don't need to set them whenever you gonna save a new entity.

Hooman
User
Posts: 9
Joined: 24-Aug-2007
# Posted on: 13-Feb-2008 16:08:10   

The thing is the row is already in the database, so it has passed all the validation because it is there. I have actually debugged the code and the field has its value. the problem happens when I try to save an existing row as a new row by changing some fields. sounds like non-relevant field converts to null! The question is whether my approach to save an existing row as a new row should work that wayconfused ?

Thanks, Hooman

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 13-Feb-2008 16:31:54   

Yes it should work if you used the following code:

foreach (IEntityField2 field in address.Fields)
{
         field.IsChanged = true;
}

The thing is the row is already in the database, so it has passed all the validation because it is there.

I was mentioning the ValidateEntityBeforeSave, just as an event to set default values for fields before saving them.

You are attempting to save an entity as a new one, and if some fields has the IsChanged flag set to false, LLBLGen Pro won't attempt to save them. So the fact that they already hold data is irrelevant here unless IsChanged flag is set to true.

That's why you should use the above piece of code.