linq2db / linq2db.EntityFrameworkCore

Bring power of Linq To DB to Entity Framework Core projects
MIT License
449 stars 39 forks source link

IMergeable.UpdateWhenMatched: How can I provide custom setters for SOME of the fields, and use default for others #362

Closed bt-88 closed 7 months ago

bt-88 commented 9 months ago

As is

Currently, for merge operations, you provide two overloads:

To be

What I would like to have is a combination of both. That is, it updates all fields that are in both source and target AND applies the custom 'overrides' for some of the fields.

Use case

Use case. Have user defined setters for the UpdatedAt and CreatedAt fields in my base entity, but use the default update setters for all other fields.

.UpdateWhenMatched((target,source)=> new TTarget
            {
                CreatedAt = target.FusionCreatedAt,
                UpdatedAt = DateTime.UtcNow,
                // Use defaults for all other fields without the need to specify them explicitly
            });

The same applies to .InsertWhenNotMatched. Here I would like to do:

.InsertWhenNotMatched( _ => new TTarget { CreatedAt = DateTime.UtcNow, // Use defaults for all other fields without the need to specify them explicitly })
bt-88 commented 7 months ago

Anyone? If someone can guide me in the right direction I'm willing to draft a PR for this.

sdanyliv commented 7 months ago

Sorry for delay, it needs API design, not simple thing right now. I can help you to write such method without changing linq2db code.

bt-88 commented 7 months ago

Sorry for delay, it needs API design, not simple thing right now. I can help you to write such method without changing linq2db code.

Thank you, that would be awesome. Please show me how when you have the time.

sdanyliv commented 7 months ago

I assume it should work only if TSource and TTarget are the same?

bt-88 commented 7 months ago

I assume it should work only if TSource and TTarget are the same?

Yes, correct.

sdanyliv commented 7 months ago

Duplicate https://github.com/linq2db/linq2db/issues/4357

sdanyliv commented 7 months ago

Added answer in linq2db repository.