Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC C# Changing Database Structure Without Dropping Database

If I add a property to a class or add an entire class to the DbSet in my database file, Entity Framework should automatically add tables columns accordingly. This is working fine, but my issue is dropping the entire database and recreating it.

The remote server I am using doesn't allow me to recreate the database through Visual Studio and I must go and make an entire new database manually if I add/change classes/properties. Also, it won't let me delete the reference to the database even though it technically has been deleted so I have to rename the new database to another name.

Is there any way that I can change the structure of my database easily without dropping and recreating it?

like image 625
user3330265 Avatar asked Aug 22 '26 16:08

user3330265


1 Answers

Yes, there is a way. Use Entity Framework migrations. Start from executing this from your package manager console:

Enable-Migrations

Here is an article on MSDN about it. You have an option of using migrations only if you use Entity Framework Code First approach.

The cool thing about EF migrations is that you have automatic migrations option that you can enable. Basically all your schema changes that would not affect data integrity or loss (e.g. adding columns or new tables) in your database can be automatically pushed without explicitly implementing migration.

like image 79
Andrei Avatar answered Aug 25 '26 06:08

Andrei



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!