Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update database on remote ms sql server (EF Code First)

While developing an application I used EF automatic migrations. So now when I have deployed my app on VPS, I don't know how to add new tables and fields to my database.

Can I connect to the remote database directly from my project in VS2012, updating a connection string, and update the database using "update-database" in package manager console? Or do I need to install VS on my VPS and update the database from VPS?

My database is already filled with data, so I can't delete it and create again.

like image 500
Ilya Leykin Avatar asked Sep 21 '13 06:09

Ilya Leykin


People also ask

How do I update my Entity Framework database first?

Right-click anywhere on the design surface, and select Update Model from Database. In the Update Wizard, select the Refresh tab and then select Tables > dbo > Student. Click Finish.

How do I change code first to database first?

There is no way to convert your code-first classes into database-first classes. Creating the model from the database will create a whole new set of classes, regardless of the presence of your code-first classes. However, you might not want to delete your code-first classes right away.

How do I update my Entity Framework database?

After creating a migration file using the add-migration command, you have to update the database. Execute the Update-Database command to create or modify a database schema. Use the –verbose option to view the SQL statements being applied to the target database.


2 Answers

Yes you can use Visual Studio, follow this tutorial - it should work for VS 2012 too.
You can also use Code first Migration to update your model by using this command in package manager console:

Update-Database

and you can specify your connection string name:

Update-Database -ConnectionStringName "MyConnectionString"
like image 100
Sirwan Afifi Avatar answered Oct 28 '22 17:10

Sirwan Afifi


This is best explained in the link below. Read the "Getting a SQL script" section. That will explain how to generate a script which you can then run on your target database.

This will be necessary if your database access is IP protected for example.

https://msdn.microsoft.com/en-us/data/jj591621.aspx

like image 24
Andy Vennells Avatar answered Oct 28 '22 15:10

Andy Vennells