What would be the best way to add methods to an Entity model.
Such-as, I have a User model and I would like to add a method to the class that would hash passwords.
I have tried making a UserHelper class that extends User but this seems almost counterproductive.
If EF classes are not already partial classes, you could modify the T4 template to make them partial classes.
A partial class is a class that’s defined across multiple files. At compile time, the compiler builds the class definition from all files. Partial classes are most commonly used to add code to classes that are generated through automated tools (and are normally destroyed each time the generation runs), such as the T4 template for EF.
With a partial class, as mentioned above, you could then add your methods on a separate file to the class you’re trying to “extend”.
Having said all of this, you may also consider extension methods.
Extension methods are a neat syntax/compiler trick to extend the functionality of types you can’t (eg. you don’t own the code or cant’ subclass them) modify. By writing an extension method, you’re basically writing a static method that will be called as if it was an instance method of the type you just extended.
I think either option is almost just as good, but I’d lean for the partial class method.