C# extending a entity framework class

centityframeworksmvc

I have a Entity Framework class that was derived from the database layout. I was wondering if there was any problems by extending the class by creating another cs file and using the same public partial class to add the additional properties?

Much like you would do when creating a file for data annotations. I have tested and it works for my MVC application. It allows me to pass some dynamic properties to the view model while still using the original model from EF 'and additional file I created'.

Does this pose any problems, or is there a better solution?

Best Answer

This is the preferred method of extending the EF data classes.

You can add almost anything. I had a situation where I needed to use different annotations (replacements for the validation, as the defaults weren't sufficient for my needs).

Microsoft provides plenty of examples to do this.