Getting “SQLExpress database file auto-creation error” for site that uses AspNetSqlMembershipProvider, but connection string is to SQL Server 2005

后端 未结 3 1532
逝去的感伤
逝去的感伤 2021-01-18 05:03

I have an ASP.NET v2.0 website (not web application) where the root directory is public, but the \"Admin\" subdirectory requires authentication. Everything abo

相关标签:
3条回答
  • 2021-01-18 05:39

    According to your comment, it looks like you have haven't explicitly configured a role provider for you site.

    If all that's in your web.config is:

    <roleManager enabled="true" />
    

    Then you are relying on the default providers declared further up the configuration hieracrchy (machine.config, global web.config, etc)

    In machine.config you've probably got something like:

    <roleManager>
      <providers>
        <add name="AspNetSqlRoleProvider" 
          connectionStringName="LocalSqlServer" 
          applicationName="/" 
          type="System.Web.Security.SqlRoleProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
        <add name="AspNetWindowsTokenRoleProvider" 
          applicationName="/" 
          type="System.Web.Security.WindowsTokenRoleProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
      </providers>
    </roleManager>
    

    As you can see, the first provider is configured to use a connectionString called LocalSqlServer - which is also usually declared in the machine.config:

    <add name="LocalSqlServer" 
         connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" 
         providerName="System.Data.SqlClient"/>
    

    And this is designed to use a local file based database that will be created if it doesn't already exist.

    So to get roles working on your site, you should ammend your root web.config to something like:

    <roleManager enabled="true">
      <providers>
        <clear />
        <add name="AspNetSqlRoleProvider" 
          connectionStringName="YourConnectionStringName" 
          applicationName="/" 
          type="System.Web.Security.SqlRoleProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
      </providers>
    </roleManager>
    

    Using the <clear /> element will remove all previously defined providers for that type.

    0 讨论(0)
  • 2021-01-18 05:48

    I had the same problem which was due to the services for sql server being disabled.

    Check under services.msc to see if the sqlexpress service is running. If it is check to see if you have sql express installed on your machine

    0 讨论(0)
  • 2021-01-18 05:49

    When you specify the AttachDBFilename option in the connection string you are in fact asking for your-very-own-just-in-time-provisioned SQL Server instance, aka the 'user instance'. This instance is created by asking the 'master' instance (the .\SQLEXPRESS instance) to provision a child instance, which implies copying the master/model/msdb into your profile, starting a new SQL Server process under your account configured to use the freshly copied master/model/msdb, then asking this 'child' instance to attach the specified 'file' as a new database. The details are explained in SQL Server 2005 Express Edition User Instances.

    The process of creating a child instance is extremly fragile and when it breaks the DB call results eventually in a time out error when opening the connection. In your case it seems that the process breaks in some cases (when you reach the protected part of the site). Why it breaks, is very hard to guess without proper information. Look at the Common Issues in the linked article and see if any applies to you. Also check the system event log for any message why the child instances cannot start or it cannot open the MDF file. Note that a common mistake is to ask for the same physical file with AttachDBFilename under different credentials: each credential will start its own 'child' instance and only the first one will succeed in attaching the desired database.

    0 讨论(0)
提交回复
热议问题