Setting NULL to update

Posts   
 
    
dodyg
User
Posts: 42
Joined: 04-Dec-2014
# Posted on: 04-Dec-2014 16:10:28   

var customer = new CustomerEntity();
customer.CustomerID="CHOPS";
customer.IsNew=false;
customer.Phone = null;
using(DataAccessAdapter adapter = new DataAccessAdapter())
{
    adapter.SaveEntity(customer);
}

This currently does not work in setting the Phone file to null.

Is there any better way to this solution than this work around (http://llblgen.com/TinyForum/Messages.aspx?ThreadID=14469&StartAtMessage=0&#80673)?

Because with the work around, pretty much I have to double up every field assignment from model object to entity object .e.g


entity.Fields[Reflector.GetMemberName<EmployerEntity>(x => x.AddressCityId)].IsChanged = true;
entity.AddressCityId = null;

And these are extremely verbose.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 04-Dec-2014 16:25:24   

llblgen pro version, runtime version would be great simple_smile http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=7717

Frans Bouma | Lead developer LLBLGen Pro
dodyg
User
Posts: 42
Joined: 04-Dec-2014
# Posted on: 04-Dec-2014 16:31:50   

Sorry..

LLBLGen 4.0. Runtime 4.

Edit: SD.LLBLGen.Pro.ORMSupportClasses v 4.0.13.621

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 04-Dec-2014 16:40:44   

Please check the link Otis posted to know how to get the runtime library version.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 05-Dec-2014 11:19:09   

The actions you want to do are meant for updating an existing entity without fetching it first. Otherwise a normal fetch, property set, save action would suffice, which doesn't require fiddling with state flags. Is this what you want to do, updating without fetching it first?

To make the runtime believe the entity is actually 'fetched' and not 'new', some things have to be done, which unfortunately mean, the entity is indeed fetched, the values in it are the ones set and it should persist these values as if you modified data from the DB. This means that - the entity flag for 'new', IsNew, has to be set to false - the Fields.State property has to be set to 'Fetched' - you have to set a field's value to the value you want to set it to. - the IsChanged flag for a field has to be set to true.

the last two are required because if you set a field to 'null' it doesn't change, its initial value is 'null' so it will be ignored.

this is rather verbose code, true. If you need this alot, you could opt for creating a small method / extension method, to do this for you.

Keep in mind that in v4 we optimized field value storage a lot, so setting the field value and setting the change flag is done now on the Fields object (entity.Fields.SetIsChanged(index, bool), entity.Fields.SetCurrentValue(index, value))

Frans Bouma | Lead developer LLBLGen Pro
dodyg
User
Posts: 42
Joined: 04-Dec-2014
# Posted on: 07-Dec-2014 13:18:55   

Is this what you want to do, updating without fetching it first?

Yes.

I'll go the extension method route. Thanks for the tips.