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

Allow mapping of get-only property one-way to database #13316

Open
roddharris opened this issue Sep 13, 2018 · 5 comments
Open

Allow mapping of get-only property one-way to database #13316

roddharris opened this issue Sep 13, 2018 · 5 comments

Comments

@roddharris
Copy link

It would be nice if we could map a get-only property from a POCO to the database so the information gets stored.

An example of this would be a Fullname property getter that computes and returns the full name of a Person. I would like that Fullname stored in the database to make it easier on those who query the database (so they don't have to compute the name). However, because it is computed in my POCO object, I don't need the data mapped back into the object by EF.

public class Person
{
    public string Firstname {get; set;}
    public string Lastname {get; set;}
    public string Fullname  => $"{this.Lastname}, {this.Firstname}" ;
}

//Pseudo EF Mapping
builder.Entity<Person>().Property(p => p.Fullname).HasColumn("fullname").StoreOnly();
@ajcvickers
Copy link
Member

@roddharris We discussed this in #7946 and decided that having an empty setter is reasonable for this scenario. For example:

public string Fullname
{
    get => $"{this.Lastname}, {this.Firstname}";
    private set { }
}

@ajcvickers
Copy link
Member

We have had several requests for this (see #7946 and #21356 as examples) so re-opening to put on the backlog.

@julealgon
Copy link

We discussed this in #7946 and decided that having an empty setter is reasonable for this scenario.

I assume you meant reasonable as a workaround, correct @ajcvickers ?

The code looks really hacky and confusing with this in place, so it would be nice if EF just detected the readonly property instead and behaved appropriately.

@MichaelRRM
Copy link

To further support @julealgon 's comment, as it stands I need not only a private setter but also a comment explaining why it is needed, and to ignore the warning as my IDE wants me to delete it.

I'm in the position of trying to "sell" EF to my company and this makes it harder.

@FLAMESpl
Copy link

Hi I have just stumbled upon this. I acknowledge the workaround but it would be nice to have an option in model builder such as

public Model
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public string NormalizedName => Name.ToUpper();
}
...
    b.Property(x => x.NormalizedName).IgnoreOnRead();
...

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

5 participants