EntityFramework-Plus icon indicating copy to clipboard operation
EntityFramework-Plus copied to clipboard

Batch Update with only non-null properties of object

Open jetompki opened this issue 6 years ago • 2 comments

I have an object that maps to a database and it can have null properties.

I don't know which properties are going to be null.

How can I issue an update on only the non-null properties of the object?

I don't want to manually code all individual properties.

I was thinking ResolveUpdateFromQueryDictValues might work, but I'm not sure how to use it.

Suggestions?

Thanks.

jetompki avatar Sep 24 '19 04:09 jetompki

Hello @jetompki ,

Are you trying to use the Batch Update feature? It normally updates all rows from the query to the same value, so I'm not sure to understand your request here. It doesn't make an Update depending on each row.

However, this request from what I understand might be easily possible with the BulkUpdate feature from our paid library Entity Framework Extensions. It possible to make some Coalesce for property value (Online Example)

Is it something that could interest you?

Best Regards,

Jonathan


Performance Libraries context.BulkInsert(list, options => options.BatchSize = 1000); Entity Framework ExtensionsEntity Framework ClassicBulk OperationsDapper Plus

Runtime Evaluation Eval.Execute("x + y", new {x = 1, y = 2}); // return 3 C# Eval FunctionSQL Eval Function

JonathanMagnan avatar Sep 24 '19 19:09 JonathanMagnan

All I need it to do is generate SQL for me that updates only the non-null properties of the entity. By non-null properties I mean nullable types that just happen to not be null.

Example:

class Entity{
val Id;
val Issue=null;
val Test = "Hello";
}

table.update(entity) => Update table Where entity.Id = id Set Test="Hello"

In my above vague SQL I purposefully removed the Issue property as it was null and I don't want to set it to null in the database.

I don't see what the option is in BulkUpdate to do that either.

Separate Note:

Do you know how to use this function that is in the BatchUpdate class? It is called ResolveUpdateFromQueryDictionary. By the name, it seems like it might be formulating an SQL update query based off of a dictionary of some sort. But maybe I'm wrong, I don't see any documentation around this function.

BatchUpdate class is used in the second override of BatchUpdate, aka...

table.where(i=>id==1).update(m=>m,BATCHUPDATE=>BATCHUPDATE.ResolveUpdateFromQueryDictValues...)
This is what the BatchUpdate class looks like generally in

namespace Z.EntityFramework.Plus{

public class BatchUpdate{
...
 public Dictionary<string, object> ResolveUpdateFromQueryDictValues<T>(Expression<Func<T, T>> updateFactory);
...
}
}

jetompki avatar Sep 24 '19 22:09 jetompki