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

前端 未结 9 1320
小鲜肉
小鲜肉 2020-12-12 17:20

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 mi

相关标签:
9条回答
  • 2020-12-12 17:25

    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">
    
    0 讨论(0)
  • 2020-12-12 17:27

    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

    0 讨论(0)
  • 2020-12-12 17:27

    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

    0 讨论(0)
  • 2020-12-12 17:29

    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.

    0 讨论(0)
  • 2020-12-12 17:31

    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.

    0 讨论(0)
  • 2020-12-12 17:31

    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>
    
    0 讨论(0)
提交回复
热议问题