Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support value conversions from two properties into one column #20582

Closed
bcallaghan-et opened this issue Apr 9, 2020 · 2 comments
Closed

Support value conversions from two properties into one column #20582

bcallaghan-et opened this issue Apr 9, 2020 · 2 comments

Comments

@bcallaghan-et
Copy link

I have a several classes that contain property pairs like the following

class MyEntity
{
    public int MyValue { get; set; }
    public bool MyValueSpecified { get; set; }
}

I would like EF to ignore MyValue and store null in its place if MyValueSpecified is false. Otherwise, MyValue should be mapped to the database as normal. The column behind this pairing should be nullable, but otherwise match the type of MyValue. To do this, I propose a new method on the model builder that allows multiple properties mapped to the same column. For example:

modelBuilder.Entity<MyEntity>()
    .Property(entity => new { entity.MyValue, entity.MyValueSpecified })
    .HasValueConversion(
        x => x.MyValueSpecified ? x.MyValue : null,
        x => x is null ? (false, default) : (true, x)
    );

I realize that this looks a lot like nullable value types, and I would rather use int? in my entities, but these classes are auto-generated by WCF and are outside of my control.

Implementing this feature probably depends on #13850.

@ajcvickers
Copy link
Member

Similar to #13947. They should probably both be implemented together,

@AndriySvyryd
Copy link
Member

AndriySvyryd commented Jul 12, 2023

Duplicate of #31234

@AndriySvyryd AndriySvyryd marked this as a duplicate of #20582 Jul 12, 2023
@AndriySvyryd AndriySvyryd marked this as a duplicate of #31234 Jul 12, 2023
@ajcvickers ajcvickers removed this from the Backlog milestone Oct 11, 2023
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants