How to transfer ASP.NET MVC Database from LocalDb

2019-01-21 05:25发布

问题:

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?

回答1:

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.

Then go and hit Schema Compare option

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

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

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



回答2:

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.



回答3:

Change the connectionString in your web.config

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

to your own database connectionString, for example :

  <connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=7.7.7.7\sql;Initial Catalog=TestDB;User ID=sa;Password=sa" />
  </connectionStrings>


回答4:

It sounds like you may want to move the data from your local database to sql server. If so, the easiest way to do this would be to back up your local database and then restore it on the server.

To back up: https://msdn.microsoft.com/en-us/library/ms187510.aspx#SSMSProcedure

To restore: https://msdn.microsoft.com/en-us/library/ms177429.aspx

EDIT:

If you need to install an instance of SQL Server: https://msdn.microsoft.com/en-us/library/ms143219.aspx



回答5:

I had the same problem and just solved this...so the main point is default connection string...which you need to modify correctly otherwise it is pointless..and impossible to connect properly. So copy all you aspnetroles...users table to online database( they should look the same as in your local database). You can compare schema(local db) with real db. It is quit well explained by "Overlord" -> Explanation

But after lets now correctly modify defaultconnection string That is my default string before modification:

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

That is my modified default string after modification:

<add name="DefaultConnection" connectionString="Data Source=servername,portnumber;Initial Catalog=AttendanceTrak;Integrated Security=False;User Id=****;Password=*****;Encrypt=True;TrustServerCertificate=False;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />

servername - should be your server. portnumber - should be your server port

It took me ages to finally get it working properly...but this small trick with default string just made it! Hops this helps



回答6:

I had a similar problem, wanting to export from a local db to remote db-server and encountered an error message I couldn't find any information on, but the answer came to me when reading this post, so I'm submitting my answer here in case anyone else has the same problem.

I set up a solution with Individual User Accounts. VS conveniently creates a db (mdf-file under App_Data) and a connectionstring in the web.config.

In all my wisdom I thought: "Why not move this to a remote server?" So I did.

I restored the mdf file on the remote server, expanded it with some simple tables for my web site, created a new connection to the db and added a new ado.net edmx-file, removed the "DefaultConnection" in the web.config and updated the reference to my new connection in the ApplicationDBContext.

Pressed play, and... no sigar (when trying to log in).

The entity type IdentityUserLogin is not part of the model for the current context.

Turns out the IdentityDbContex prefers the "DefaultConnection" with the providerName="System.Data.SqlClient" so adding a new edmx-file with the providerName="System.Data.EntityClient" is no good.

Solution: As warheat1990 suggested, I updated (put back) the DefaultConnections and it's connectionstring value.

One might argue that I should have two seperate db's (one for users) and one for business stuff, but that's an other discussion.



回答7:

In relation to OverLords answer, it worked perfectly for me thanks!

If anyone is struggling with the connection string use:

    <add name="CONNECTIONSTRINGNAME" connectionString='data source= DATABASE SOURCE initial catalog=&quot;DATABASE NAME &quot;;user id=&quot;USERID&quot;;password=PASSWORD;MultipleActiveResultSets=True;' providerName="System.Data.SqlClient" />


回答8:

You can't back up you LocalDB like that. There is no SSMS interface for doing a backup there. You will have to make a copy of the localDB's MDF file and attach that to SQL Server Express (or higher). Then you can either move the files or do a backup-restore.