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:
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>
Remove RoleManager
provider in module definition.
<modules>
<remove name="RoleManager" />
</modules>
Comment out all membership
, profile
& roleManager
elements if necessary to do so.
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)