2

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.

MTplus
  • 2,077
  • 4
  • 34
  • 51
  • Where do you intend to use these other properties, in another layer? You probably want a new class which maps data across from your 'entity class' which is bound to your database model. – d219 Dec 03 '19 at 15:29
  • Partial classes were created to make extending autogenerated code easier. The specifics matter though. Adding some calculated properties, or methods that make sense in the data layer can be done using partial classes. Adding properties only to use them in the UI on the other hand, is a *bad* idea. – Panagiotis Kanavos Dec 03 '19 at 15:36
  • As for where to add the class, right next to the current class, in the same namespace. They are the *same* class split into two files. If you think they are different, you shouldn't use partial classes in the first place – Panagiotis Kanavos Dec 03 '19 at 15:38

4 Answers4

1

It's possible that when you add the partial class in your "Partial" folder, Visual Studio adds .Partials to the namespace of the partial class you add, because the convention is that namespaces should follow the folder path. In that case, the compiler won't join the auto-generated class with the partial class you created, because they are in different namespaces.

Make sure the namespace is the same in your partial class and the generated class.

Tsahi Asher
  • 1,767
  • 15
  • 28
1

You need to make sure that the namespace that you specify for your hand-written partial class User is exactly the same as that of the EF6-generated partial class User. If the namespaces between the two are different then the partial classes will not be put together, they will stay separate.

Peter B
  • 22,460
  • 5
  • 32
  • 69
  • Hi, I tried this but the property still not show up so I went for @Praveen M solution below. – MTplus Dec 04 '19 at 07:11
0

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;
Praveen M
  • 443
  • 2
  • 11
  • If this is used outside of the data layer you are binding non data layer objects to your data layer though, you may not want to do that. – d219 Dec 03 '19 at 15:30
  • 2
    Partial classes exist to extend autogenerated code. The bad idea is adding inheritance when there's no real `is-a` relation, *especially* in ORM entities – Panagiotis Kanavos Dec 03 '19 at 15:31
  • I was not sure about the real requirement here . Your statement makes total sense if there's not real "is-a" relation – Praveen M Dec 03 '19 at 15:34
0

In your User.cs you can add properties that won't be mapped to the database by using the [NotMapped] attribute above your properties:

public class User
{
    //Your autogenerated properties

    [NotMapped]
    public bool SomeBoolProperty { get; set; }

    [NotMapped]
    public int AnotherProperty { get; set; }
}
Ryan Gaudion
  • 695
  • 1
  • 8
  • 22
  • 1
    Isn't the whole class file getting autogenerated though (in which case any modification will be lost when it is next generated)? – d219 Dec 03 '19 at 16:18
  • You won't need to keep generating the class. You generate it once when you create the table. If you add any fields to your table you can then manually add them to the class by adding normal properties. – Ryan Gaudion Dec 03 '19 at 16:32