简体   繁体   中英

How to deploy ASP.NET website with SQL Server database (membership enabled) in Godaddy using VS2013

I know this question is already answered but none solve my problem, I maybe having certain unknown issue. So I am again asking this question. Please help…. thanks in advance.

I am using Visual Studio 2013 update 3. I have developed an ASP.NET web application with membership enabled that uses the LocalDb provided by VS2013. I have published the website to GODADDY web server but I am not able to deploy the database. Without the database my application is working fine. I have created my SQL Server database in the GODADDY server. I am using the Build -> Publish option available in VS2013.

My web.config file is as

<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="entityFramework" 
             type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
  </configSections>
  <connectionStrings>
    <add name="DefaultConnection" 
         connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-WebTest-20150416011010.mdf;Initial Catalog=aspnet-WebTest-20150416011010;Integrated Security=True" 
         providerName="System.Data.SqlClient"/>
  </connectionStrings>
  <system.web>
    <authentication mode="None"/>
    <compilation debug="true" targetFramework="4.5.2"/>
    <httpRuntime targetFramework="4.5"/>
    <customErrors mode="Off"/>
    <trust level="Full"/>
    <pages>
      <namespaces>
        <add namespace="System.Web.Optimization"/>
        <add namespace="Microsoft.AspNet.Identity"/>
      </namespaces>
      <controls>
        <add assembly="Microsoft.AspNet.Web.Optimization.WebForms" namespace="Microsoft.AspNet.Web.Optimization.WebForms" tagPrefix="webopt"/>
      </controls>
    </pages>
    <membership>
      <providers>
        <clear/>
      </providers>
    </membership>
    <profile>
      <providers>
        <clear/>
      </providers>
    </profile>
    <roleManager>
      <providers>
        <clear/>
      </providers>
    </roleManager>
    <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>
  </system.web>
  <system.webServer>
    <httpErrors errorMode="Detailed"/>
    <asp scriptErrorSentToBrowser="true"/>
  </system.webServer>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed"/>
        <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="WebGrease" culture="neutral" publicKeyToken="31bf3856ad364e35"/>
        <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="EntityFramework" publicKeyToken="b77a5c561934e089"/>
        <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0"/>
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="mssqllocaldb"/>
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/>
    </providers>
  </entityFramework>
</configuration>

My web.Release.config file as

<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">     
    <connectionStrings>
        <add name="DefaultConnection"
             connectionString="Data Source=xxx.xxx.xxx.xxx; Initial Catalog=mydatabase; User ID=username; Password=password"
             xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
    </connectionStrings>  
    <system.web>
        <compilation xdt:Transform="RemoveAttributes(debug)" />    
    </system.web>
</configuration>

Finally did it here it is Before uploading do the following:

Goto Models open IdentityModels.cs and change the DefaultConnection to LocalSqlServer

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("LocalSqlServer", throwIfV1Schema: false)
    {
    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }
 }

In your web.config after uploading change this

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

to

<connectionStrings>
   <remove name="LocalSqlServer" />
      <add name="LocalSqlServer" connectionString="Data Source=111.111.111.111;   Initial Catalog=myDB; User ID=username; Password=password" providerName="System.Data.SqlClient"/>    
</connectionStrings>

and

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

to

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

Now goto your godaddy account and open Websites & Domains ---> ASP.NET Settings and add the LocalSqlServer connection string there if not added. The connection string must be same as it is in the web.config file

For the database part you need to execute the script manually on the server through your database account in godaddy

The easiest way that recent versions of membership are deployed is by Code First migrations .

ie Provided that the Sql connection credentials have DBO access to your Godaddy database, the tables will be created as soon as membership functionality is required when you access it via your website.

If the connection doesn't have DBO access, an alternative way would be to Script the DDL to a .SQL file) for the the AspNet* membership tables from your local database and then run the same script on your Godaddy database using SSMS with a login which does have DBO access. You just need the following tables:

  • AspNetRoles
  • AspNetUserClaims
  • AspNetUserLogins
  • AspNetUserRoles
  • AspNetUsers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM