Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practice for editing MVC3 auto generated code?

Im sorry if this is in the wrong place, however I think it is.

So I when creating an MVC3 internet application I used a database first method and my classes were generated for me. I am new to MVC3 and fairly new to c# and visual studio although I do have a little experience.

The auto-generated code warns that editing may cause unexpected results and the changes will be removed if the code is regenerated. As I understand it that means changes to the database, rerunning the tt files or updating the model.

However is it ok practice to edit it, and just keep a track of changes incase i need to regenerate, or is there a better way to go about it?

Thanks for any help.

like image 906
Bohdi Avatar asked Aug 29 '12 12:08

Bohdi


2 Answers

The classes generated should be partial classes so you should be able to externally manage enhancements by making your own partial class with the same namespace and name.

like image 174
hunter Avatar answered Nov 06 '22 20:11

hunter


In the model first approach, using the default code generation strategy on you EDMX file, the classes generated for you DbContext and Entity classes are all partial classes.

So, depending on your type of changes, if you want to extend these generated classes, just create a new partial class with you custom code in it. Your partial's will not be overwritten by the code generator.

like image 1
Quinton Bernhardt Avatar answered Nov 06 '22 20:11

Quinton Bernhardt