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

9

46

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?

Hospitium answered 18/3, 2015 at 2:34 Comment(1)
LocalDB is a developer-oriented version of SQL Server .....Dandy
H
25

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.

Hospitium answered 18/3, 2015 at 6:18 Comment(1)
For as common as this, it sure isn't a direct answer to find. I'm wondering if you had to change the defaultConnectionFactory? I tried it both ways, appears to work both ways.Foulness
G
52

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

Grasping answered 31/10, 2016 at 6:42 Comment(3)
Great! Thanks so much : )Hawkins
Used the Schema Compare as an opportunity to test login methods.. Created new SQL Security Logins as both owner & security admin for the specified database to connect using standard SQL Login for testing, instead of using Windows login. Should work for any network database. Wish I could drop on +10 for this answer. It is the epitome of excellence & completeness!Tiflis
Schema Comparison was failing for me, then read in another post that target DB should be named as source DB and finally it worked.Glossographer
H
25

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.

Hospitium answered 18/3, 2015 at 6:18 Comment(1)
For as common as this, it sure isn't a direct answer to find. I'm wondering if you had to change the defaultConnectionFactory? I tried it both ways, appears to work both ways.Foulness
B
6

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>
Bobine answered 18/3, 2015 at 3:11 Comment(2)
Thanks! But your code doesn't work. VS says 'data source' key is not recognized. Thou changing the connection string in the right way does it. Please see my own answer to this post for the answer.Hospitium
@Hospitium maybe because providerName="System.Data.SqlClient" is absent in warheat1990's codeHomeroom
L
3

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

Lactone answered 18/3, 2015 at 3:45 Comment(0)
T
2

Overlord's migration example is spot on. My note at the end was a bit big for a comment, so here are the required changes to the web.config file. An old method on a local drive was to specify

Data Source=".\[InstanceName]

but may not work on newer interfaces, so replace [.\instance] with [ComputerName\instance] if you migrate forward. This is Visual Studio Pro 2017, SQL Server 2014 & Entity Framework 6.0.

1st update the connection string.. replace items in brackets with info needed to connect to your database.

<connectionStrings>
   <add name="DefaultConnection" 
    connectionString="Initial Catalog=[DatabaseName];Integrated Security=True;User ID=[SQLASPNETUserName];Password=[UserPassword];"
    providerName="System.Data.SqlClient" />
</connectionStrings>

next update the entity info.. The [InstanceName] used for SQL Server can be found from [SQL Server Mgmt] console - [Server Properties] - [Advanced] - [Filestream Share Name] & defaults as [MSSQLSERVER].

<entityFramework>
  <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
     <parameters>
        <parameter value="[ServerName]\[InstanceName]"/>
     </parameters>
  </defaultConnectionFactory> 
  <providers>
     <provider invariantName="System.Data.SqlClient" 
      type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/>
  </providers>
</entityFramework>

for cloud or other multi-server database migrations, also review [sessionState] settings in web.config & replace [InProc] with [Custom]. [sessionState] comes between [/roleManager] & [/system.web]

this default for 1 db server

<sessionState mode="InProc" customProvider="DefaultSessionProvider">
  <providers>
    <add name="DefaultSessionProvider"
      type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
      connectionStringName="DefaultConnection"/>
  </providers>
</sessionState>

& this replacement for mult-server or cloud environments

<sessionState mode="Custom" customProvider="DefaultSessionProvider">
Tiflis answered 27/3, 2019 at 1:0 Comment(0)
H
1

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

Harrier answered 3/5, 2018 at 11:8 Comment(0)
F
1

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" />
Fiume answered 19/10, 2018 at 9:12 Comment(0)
R
0

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.

Rh answered 6/9, 2018 at 7:53 Comment(0)
A
0

This works for me.. Change the connection string in the web config file pointing to the database server, then run the application and register a user. Once registered successfully, go to SSMS and refresh the database and then the identity tables should appear.

Regards

Arnie answered 5/3, 2019 at 15:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.