Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to transfer ASP.NET MVC Database from LocalDb to SQL Server?

I created a new ASP.NET MVC 5 project in Visual Studio 2013 (Express for Web) and by default, the project uses LocalDb as its database, but how do you transfer or migrate the database to SQL Server?

I want to use SQL Server for the database instead of LocalDb. But how?

like image 315
doncadavona Avatar asked Mar 18 '15 02:03

doncadavona


People also ask

Can we connect MVC to database?

Click on the Create button and it will update the Index view as well add this new record to the database. Now let's go the SQL Server Object Explorer and refresh the database. Right-click on the Employees table and select the View data menu option. You will see that the record is added in the database.

Is SQL Server LocalDB?

Microsoft SQL Server Express LocalDB is a feature of SQL Server Express targeted to developers. It is available on SQL Server Express with Advanced Services. LocalDB installation copies a minimal set of files necessary to start the SQL Server Database Engine.


2 Answers

Notwithstanding this question is old, the answer didn't help me so I want to share how I solved it for my self.

On Server Explorer, find your ASPNet DB. Then open it using SQL Server Object Explorer.

enter image description here

Then go and hit Schema Compare option

Compare Schemas

Then on the the Schema Compare window for the Target database, select the SQL Server data base you want the ASPNet DB to integrate to. Then hit Compare button

Set Options

Deselect all Delete actions for the target database, and leave selected all Add actions for the ASPNet DB, then hit Update button.

Update

Finally, update your connection string so it points to your SQL Server DB

like image 94
Overlord Avatar answered Sep 27 '22 21:09

Overlord


Got it!

Based on @warheat1990's answer, you just have to change the connection string. But @warheat1990's answer had a little too much change. So here's my original (LocalDb) connection string:

<add name="DefaultConnection"      connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-my_project-20150318100658.mdf;Initial Catalog=my_project-20150318100658;Integrated Security=True"      providerName="System.Data.SqlClient"/> 

To connect it to SQL Server instead of LocalDB, I modified the connection string into:

<add name="DefaultConnection"      connectionString="Data Source=SERVERNAME\SQLEXPRESS;Initial Catalog=my_project;Integrated Security=True"      providerName="System.Data.SqlClient"/> 

Thanks to @warheat1990 for the idea of simply changing the Web.config. My first thoughts were to identify and use the feature that VS supplies, if theres any. Because Microsoft doesnt have a concise documentation on how to do this.

like image 36
doncadavona Avatar answered Sep 27 '22 21:09

doncadavona