Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF Regenerate Code Models from Database Change

I'm playing around with EF and have a model set up, generated from a database. The model is code-only, no designers.

I added a field to one of my database tables.

Is there any easy way I can "regenerate" the model code?

I guess if I had used the designers, this would be easier, and I can find info on migrating the opposite way (add a model property and get a migration to update the db), but nothing about how I can propagate changes from the DB through to the model.

I supposed I could do it manually, but that hardly seems ideal...

like image 944
jleach Avatar asked Jan 20 '26 08:01

jleach


2 Answers

One option is just delete the auto generated classes from the project and once again generate them. While following this approach only thing we need to make sure that is, we should give the same name for the data model(class name which inherits from DbContext ) as the previous one.

like image 54
Jaydeep Valecha Avatar answered Jan 22 '26 22:01

Jaydeep Valecha


I remember doing something like this a while back ago - its called reverse engineer code first. Basically it looks at the current database and creates the POCO classes for you based on that database. I've done it both when creating a new project as this tutorial suggests: https://msdn.microsoft.com/en-us/library/jj200620.aspx

and I've also done it using an older version of this tool: https://visualstudiogallery.msdn.microsoft.com/72a60b14-1581-4b9b-89f2-846072eff19d

In the end, you may find that Jaydeep's method of deleting and starting over is easiest.

like image 45
Sam Talbot Avatar answered Jan 22 '26 20:01

Sam Talbot



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!