0

Writing an MVC5 app. For some reason, a local App_Data\ASPNETDB.MDF db keeps being created, even though in my web.config the only db I specify is a totally different one on the network (and is working fine.) What could be creating this db (and recreating, if I delete it)? I searched my solution for ASPNETDB.MDF and nothing appears, so it's not in any type of config file, etc....

The connectionStrings section of my web.config....

<connectionStrings>
  <remove name="ZZZ_SpringContext" />
  <add name="ZZZ_SpringContext" 
       connectionString="Data Source=mypc\sql2012;Initial Catalog=ZZZ_Spring;Integrated Security=True;MultipleActiveResultSets=True" 
   providerName="System.Data.SqlClient" />

</connectionStrings>

***** UPDATE *****

I tried deleting the db again. Then in my web.config I changed the rolemanager line to false and the db is NOT recreating now. That helps me to see what is creating the db. But, how can I get around this? Here's the line in the web.config....

    <roleManager enabled="false" />

I had to change it back to "true" so my app would work correctly.

WebDevGuy2
  • 1,159
  • 1
  • 19
  • 40
  • Maybe the AspNet Identity? What tables do you see when you open the database using Visual Studio or SSMS? – maxbeaudoin May 24 '17 at 01:37
  • @maxbeaudoin I am using Identity but all tables are in the db defined in the web.config. I couldn't view the contents of the ASPNETDB.MDF db. I included it in my project and went to open and it said "This db file is not compatible with the current instance of SQL Server." Every time I delete it and run the app it is recreated. – WebDevGuy2 May 24 '17 at 01:49
  • Can you include the connectionStrings section of your web.config? – maxbeaudoin May 24 '17 at 01:53
  • @maxbeaudoin I included it above. Thanks – WebDevGuy2 May 24 '17 at 02:00
  • @maxbeaudoin See my update above – WebDevGuy2 May 24 '17 at 02:17
  • 1
    Generated ASPNETDB database often comes from `Membership` or `SimpleMembership` provider with automatic DB creation enabled, which somehow uses `Membership` functionality before completing DB initialization process, which created ASPNETDB database in `App_Data` folder. Include your controller code which possibly contain `Authorize` or `InitializeSimpleMembership` attribute. – Tetsuya Yamamoto May 24 '17 at 02:28

1 Answers1

2

In the past I had nearly same problem where ASPNETDB.mdf file still recreate even it already deleted. It turns that ASP.NET Identity instance uses machine.config file outside the project which has the following line:

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

And in the end of configuration file I found the membership element, which seems controlling Membership provider behavior:

<system.web>
    <membership>
        <providers>
            <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="LocalSqlServer" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="true" applicationName="/" requiresUniqueEmail="false" passwordFormat="Hashed" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="7" minRequiredNonalphanumericCharacters="1" passwordAttemptWindow="10" passwordStrengthRegularExpression=""/>
        </providers>
    </membership>
    <profile>
        <providers>
            <add name="AspNetSqlProfileProvider" connectionStringName="LocalSqlServer" applicationName="/" type="System.Web.Profile.SqlProfileProvider, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
        </providers>
    </profile>
    <roleManager>
        <providers>
            <add name="AspNetSqlRoleProvider" connectionStringName="LocalSqlServer" applicationName="/" type="System.Web.Security.SqlRoleProvider, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
            <add name="AspNetWindowsTokenRoleProvider" applicationName="/" type="System.Web.Security.WindowsTokenRoleProvider, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
        </providers>
    </roleManager>
</system.web>

Note that connectionStringName="LocalSqlServer" refers to ASPNETDB.mdf as shown in connectionStrings element before.

Then, in web.config inside the project, this line determines if RoleManager instance is enabled:

<roleManager enabled="true">
    <providers>
      <clear/>
      <add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="ApplicationServices" applicationName="/" />
      <add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/" />
    </providers>
</roleManager>

The lines given above tells that ASP.NET Membership provider is enabled as current user management, which defeats purpose of ASP.NET Identity.

Hence, if the ASPNETDB.mdf database is not exist & Membership provider support being enabled, the Membership provider will initialize with default configuration settings in machine.config, and automatically generates ASPNETDB.mdf and its log file using default table definitions.

To prevent re-creation of that DB, in addition to change <roleManager enabled="false">, you can use these steps in web.config:

  1. Clear existing or predefined connection strings before defining your own.

    <connectionStrings>
       <clear />
       <remove name=LocalSqlServer /> <== this is maybe optional
       <add name="ZZZ_SpringContext" 
       connectionString="Data Source=mypc\sql2012;Initial Catalog=ZZZ_Spring;Integrated Security=True;MultipleActiveResultSets=True" 
       providerName="System.Data.SqlClient" />
    </connectionStrings>
    
  2. Remove RoleManager provider in module definition.

    <modules>
      <remove name="RoleManager" />
    </modules>
    
  3. Comment out all membership, profile & roleManager elements if necessary to do so.

  4. If your account model code has UsersContext class, ensure it pointed to current connection string defined on your own.

    public class UsersContext : DbContext
    {
        public UsersContext() : base("ZZZ_SpringContext")
        {
        }
    }
    

Related issue:

How do I stop using ASPNETDB.MDF in LocalDB? (WebForms version)

Tetsuya Yamamoto
  • 24,297
  • 8
  • 39
  • 61