Search code examples
c#partial-classes

extend class in c# with properties


I use Entity framework 6 (database first) in my mvc project. The database has a table called User which has columns like name, adress, email. In my project I therefore have the User.cs class auto generated in my project. I need to extend that user class with some properties. Should I create this in order to extend the class with this prpoerty..?

    public partial class User
{
    public bool SomeBoolProperty { get; set; }
}

If so where do I add it? I tried to add it under a folder called partials\user.cs but when doing so this property is not available when looking at what properties is available.


Solution

  • You can have a child class derived from User class .

    Ex:

    public class childClass:User
    {
       public bool SomeBoolProperty { get; set; }
    }
    

    You can access these property like this

    User _user= new ChildClass();
    _user.SomeBoolProperty =10;