Efcore: AddOrUpdate extension method

Created on 23 Jul 2014  路  7Comments  路  Source: dotnet/efcore

Hi all,

I don't know if this method is planned or not for EF7, but in the case it will be added, please do not implement it as EF6 version! The AddOrUpdate feature that overrides all other properties (those properties that has not been set in the object passed to the method) with default value is really counter intuitive and not useful at all.

Cheers, Max

Most helpful comment

We don't have specific plans around AddOrUpdate yet. This problem is generally hard though because we don't know which properties to update. For example, does a null string mean set it to null or not update it. Int isn't nullable so does zero mean update to zero or ignore it.

@rowanmiller @ajcvickers I really don't understand why this is such a difficult thing to implement.
IMHO AddOrUpdate should act as a simple plain replace if the entity already exists.
The right call here is to use the full state of the object beeing inserted/updated, not trying to guess which properties should be updated or not.

All 7 comments

Hey Max,

We don't have specific plans around AddOrUpdate yet. This problem is generally hard though because we don't know which properties to update. For example, does a null string mean set it to null or not update it. Int isn't nullable so does zero mean update to zero or ignore it.

One way around this is to ask for a list of properties that need updating. Would that meet your requirements?

We should look at this property list approach for the DbContext.Update method.

~Rowan

Added note to https://github.com/aspnet/EntityFramework/issues/323 about considering a whitelist of properties to DbSet/DbContext.Update.

Also added notes about AddOrUpdate to https://github.com/aspnet/EntityFramework/issues/629

@rowanmiller , for the entity framework core 1.1. I still don't see AddOrUpdate method. Could you tell me whether it has been implemented yet ?

@redplane There are no concrete plans to implement AddOrUpdate at this time. The way it was implemented in EF6 had issues with identity resolution. Anything we do on EF Core is likely to work differently, and it's not clear that it would have enough value over making your own choice as to whether an entity should be added or updated.

Hi @ajcvickers , I think in AddOrUpdate there should be a list of white list properties as @rowanmiller mentioned. If no property is specified in white list, that means every property should be updated.

We don't have specific plans around AddOrUpdate yet. This problem is generally hard though because we don't know which properties to update. For example, does a null string mean set it to null or not update it. Int isn't nullable so does zero mean update to zero or ignore it.

@rowanmiller @ajcvickers I really don't understand why this is such a difficult thing to implement.
IMHO AddOrUpdate should act as a simple plain replace if the entity already exists.
The right call here is to use the full state of the object beeing inserted/updated, not trying to guess which properties should be updated or not.

@thiagomajesk Maybe it's a scant comfort to know that AddOrUpdate in EF6 had its own problems, considerably limiting its usefulness.

Was this page helpful?
0 / 5 - 0 ratings