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

I have a update question

Open Leowuqunqun opened this issue 8 years ago • 12 comments

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?

Leowuqunqun avatar Dec 08 '17 03:12 Leowuqunqun

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 avatar Dec 08 '17 14:12 JonathanMagnan

@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

Leowuqunqun avatar Dec 12 '17 08:12 Leowuqunqun

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

JonathanMagnan avatar Dec 12 '17 13:12 JonathanMagnan

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?

Leowuqunqun avatar Dec 13 '17 04:12 Leowuqunqun

@JonathanMagnan I think this is reasonable, I do not know what you think?

Leowuqunqun avatar Dec 13 '17 04:12 Leowuqunqun

Hello @Leowuqunqun ,

So if I understand your request correctly, using your first example:

  • You want to update a car using the key (which can be found in the entity) and update some specific property directly in the database without having to use SaveChanges?

Best Regards,

Jonathan


Help us to keep this library free: Donate

JonathanMagnan avatar Dec 18 '17 20:12 JonathanMagnan

@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

Leowuqunqun avatar Dec 19 '17 05:12 Leowuqunqun

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.

granthoff1107 avatar Dec 30 '17 02:12 granthoff1107

@JonathanMagnan
How is this problem going?

Leowuqunqun avatar Jan 18 '18 14:01 Leowuqunqun

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

JonathanMagnan avatar Jan 28 '18 15:01 JonathanMagnan

Is there a way I can do a bulk delete but it is committed only when I call SaveChanges?

barishamil avatar Mar 08 '18 13:03 barishamil

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

JonathanMagnan avatar Mar 08 '18 13:03 JonathanMagnan