"建立与SQL Server的连接时发生与网络相关或特定于实例的错误。"

时间:2014-12-18 19:03:18

标签: c# asp.net-mvc azure azure-sql-database

我目前陷入了一种我不知道如何去做的情况。对于我正在处理的网站,如果我没有用户登录,我可以访问需要使用数据库信息的页面。当我登录时,我收到标题中指定的错误。

我的数据库和网站使用免费试用版通过Azure云服务托管,我也在web.config中启用了角色管理器(禁用它可以解决错误,但我需要启用它来进行角色检查)。当我在本地主机时,一切正常,所以只有当我的网站部署时我才会遇到问题。

我是ASP.NET的新手,所以我仍然迷失在其工作原理的一些基础上。

**编辑:**这是我的web.config文件,使用timeiscoffee添加的代码

    <?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=301880
  -->
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <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-Entertainist-20141207110857.mdf;Initial Catalog=aspnet-Entertainist-20141207110857;Integrated Security=True" providerName="System.Data.SqlClient"  />
  </connectionStrings>
  <appSettings>
    <add key="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>
  <system.web>
    <customErrors mode="Off" />
    <authentication mode="None" />
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
    <roleManager enabled="true"
    defaultProvider="SqlRoleProvider">
      <providers>
        <add name="SqlRoleProvider" type="System.Web.Security.SqlRoleProvider"
         connectionStringName="DefaultConnection"
         applicationName="Entertainist"/>
      </providers>
    </roleManager>
    <globalization culture="en-US" uiCulture="auto:en-US" />
  </system.web>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
      <remove name="FormsAuthentication" />
    </modules>
  </system.webServer>

  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="CalculatorServiceBehavior">
          <serviceAuthorization principalPermissionMode="UseAspNetRoles"
                                roleProviderName="SqlRoleProvider" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>


  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin.Security.OAuth" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin.Security.Cookies" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin.Security" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <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="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-5.2.2.0" newVersion="5.2.2.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
</configuration>

这是我的Configuration.cs文件,它设置角色并将种子数据添加到数据库中

    namespace Entertainist.Migrations
{
    using Entertainist.Models;
    using Microsoft.AspNet.Identity;
    using Microsoft.AspNet.Identity.EntityFramework;
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.Linq;

    internal sealed class Configuration : DbMigrationsConfiguration<Entertainist.Models.ApplicationDbContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = true;
            ContextKey = "Entertainist.Models.ApplicationDbContext";
        }

        protected override void Seed(Entertainist.Models.ApplicationDbContext context)
        {

            var UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
            var RoleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));

            // Create Admin Role
            string roleName = "Admins";
            IdentityResult roleResult;

            // Check to see if Role Exists, if not create it
            if (!RoleManager.RoleExists(roleName))
            {
                roleResult = RoleManager.Create(new IdentityRole(roleName));
            }

            context.Artists.AddOrUpdate(
                a => a.ArtistName,
                new Artist { ArtistName = "Kanye West" },
                new Artist { ArtistName = "65daysofstatic"},
                new Artist { ArtistName = "D'Angelo"},
                new Artist { ArtistName = "Faith No More"},
                new Artist { ArtistName = "Daft Punk" },
                new Artist { ArtistName = "Lana Del Rey"},
                new Artist { ArtistName = "The Decemberists"},
                new Artist { ArtistName = "The War On Drugs"}
                );

            context.MusicGenres.AddOrUpdate(
                g => g.GenreName,
                new MusicGenre { GenreName = "Rock" },
                new MusicGenre { GenreName = "Hip-Hop" },
                new MusicGenre { GenreName = "Electronic" },
                new MusicGenre { GenreName = "Country" },
                new MusicGenre { GenreName = "Classical" },
                new MusicGenre { GenreName = "Indie"},
                new MusicGenre { GenreName = "Rap"},
                new MusicGenre { GenreName = "Metal"},
                new MusicGenre { GenreName = "Pop"}
                );

            context.MovieGenres.AddOrUpdate(

                g => g.GenreName,
                new MovieGenre { GenreName = "Adventure"},
                new MovieGenre { GenreName = "Action"},
                new MovieGenre { GenreName = "Anime"},
                new MovieGenre { GenreName = "Bollywood"},
                new MovieGenre { GenreName = "Sci-Fi"},
                new MovieGenre { GenreName =" Independent"},
                new MovieGenre { GenreName = "Fantasy"}

                );


            context.Studios.AddOrUpdate(
                s => s.StudioName,
                new Studio { StudioName = "Universal"},
                new Studio { StudioName = "MGM"},
                new Studio { StudioName = "Disney"},
                new Studio { StudioName = "Bad Robot"},
                new Studio { StudioName = "Studio Ghibli"},
                new Studio { StudioName = "Pixar"}
                );

            context.Directors.AddOrUpdate(
                d => d.DirectorName,
                new Director { DirectorName = "Steven Spielberg"},
                new Director { DirectorName = "Michael Bay"},
                new Director { DirectorName = "James Cameron"},
                new Director { DirectorName = "Ang Lee"},
                new Director { DirectorName = "J.J. Abrams"},
                new Director { DirectorName = "Hayao Miyazaki"}

                );

            //  This method will be called after migrating to the latest version.

            //  You can use the DbSet<T>.AddOrUpdate() helper extension method 
            //  to avoid creating duplicate seed data. E.g.
            //
            //    context.People.AddOrUpdate(
            //      p => p.FullName,
            //      new Person { FullName = "Andrew Peters" },
            //      new Person { FullName = "Brice Lambson" },
            //      new Person { FullName = "Rowan Miller" }
            //    );
            //
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您是否已将RoleProvider扩展为使用您的azure云服务的数据库?如果没有,它可能会尝试使用sqlexpress,这就是为什么它可以在你的本地工作但不在部署时。

http://msdn.microsoft.com/en-us/library/aa702542(v=vs.110).aspx