I have a update question
public int UpdateNonDefaults(T entity, Expression<Func<T, bool>> where, bool async = false)
{
_context.Set<T>().Where(where).Update(a => entity);
return async
? _context.SaveChangesAsync().GetAwaiter().GetResult()
: _context.SaveChanges();
}
var updateResult = c.UpdateNonDefaults(car, a => a.Age == 10);
please read my code , why can not directly update the entity?
Hello @Leowuqunqun ,
The Update method requires an expression such as .Update(x => new User() { IsSoftDeleted = 1 });
The goal of BatchUpdate feature is updating entities without loading them in the context. You already have SaveChanges is you want to update a single entity.
I'm not sure what is your current expectation, perhap you could give us more information.
Best Regards,
Jonathan
Help us to keep this library free: Donate
@JonathanMagnan Thank you for taking the time to look at my question. Your update method need a memberinitexpression . My code is generated memberexpression . Normally, I use AutoMapper Map to the entity that I want to modify ,Not written Lamda
Hello @Leowuqunqun ,
I'm not sure to understand correctly your last message.
Could you be more specific or provide a code example about how you would like it to work even if that doesn't compile?
Best Regards,
Jonathan
Help us to keep this library free: Donate
Hello @JonathanMagnan no problem example :
var A = AutoMapper.Mapper.Map<entity.A>(input);
result = Repository.UpdateNonDefaults(A, item => A.Id==0);
input is a inputDTO ,map to A through AutoMapper. A contains the field I want to change. Can I use A directly?
@JonathanMagnan I think this is reasonable, I do not know what you think?
Hello @Leowuqunqun ,
So if I understand your request correctly, using your first example:
- You want to update a
carusing the key (which can be found in the entity) and update some specific property directly in the database without having to useSaveChanges?
Best Regards,
Jonathan
Help us to keep this library free: Donate
@JonathanMagnan
I do not want to pass the primary key。 "A.Id" only a example Use primary key need to check first . Any one or more properties to query can determine the uniqueness
Correct me if I'm wrong but @Leowuqunqun you want to use automapper so you can pass an instance to the update.
@JonathanMagnan, I'm not sure if the library works this way but this line Update(x => new User() { IsSoftDeleted = 1 }) here you use the expression to figure out how to write the sql. But Leo want to pass an instance of the already created automapped version and the values transformed from that to create the expression instead.
@JonathanMagnan
How is this problem going?
Hello @Leowuqunqun ,
Sorry for the delay,
To be honest, we are starting to put all ameliorations on hold to re-work first the core to make it easier to provide some improvement in the future. I believe a roadmap will be soon available.
Unfortunately, that means it will take a few months before we start to work on some added features like this one.
Best Regards,
Jonathan
Is there a way I can do a bulk delete but it is committed only when I call SaveChanges?
Hello @barishamil ,
Please create your own thread if the question is not related at all at the current thread.
In short, no, unless you want something like a transaction.
Best Regards,
Jonathan