Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom properties in EF database first

Good day!

I have created an EF model from database using database first aproach, and added myself several read only properties to entity class generated by EF which are not in database. Every time I update my model adding data from new tables I loose properties created, so I have to recreate them.

As an example in database I have property isFemale but in my class I've created

public string Gender
{
    get
    {
           if(isFemale) return "female";
           else return "male";
     }
}

My question is there a way to update the model from database, leaving properties generated by me?

Thank you!

like image 802
Dovlet Mamenov Avatar asked Mar 10 '26 18:03

Dovlet Mamenov


1 Answers

Add the properties on another Partial Class instead of the generated class. For example, if your generated class is of type Person, define another Partial class in the same project with the same namespace:

public partial class Person
{
    public string Gender
    {
        get
        {
            if(isFemale) return "female";
            else return "male";
         }
    }
}
like image 111
Richard Avatar answered Mar 13 '26 08:03

Richard