How to stop using ASPNETDB.MDF in LocalDB?

I implemented the ASP.NET identifier and it automatically created ASPNETDB.MDF and aspnetdb_log.ldf in the App_Data folder. I already have AspNet tables (i.e. AspNetRoles, AspNetUsers, etc.) in my SQL Express instance (where all my other tables are located). As far as I can tell, my application is reading and writing membership and role data from an SQL Express database, not ASPNETDB.MDF.

I set connectionString in web.config for:

<add name="DefaultConnection" connectionString="Data Source=MyComputerName\SQLEXPRESS;Initial Catalog=MyDatabaseName;Integrated Security=True" providerName="System.Data.SqlClient" /> 

However, if I remove ASPNETDB.MDF from App_Data, I get the following error on login:

Exception Details: System.Data.SqlClient.SqlException: One or more files do not match the main database file. If you are trying to mount the database, repeat the operation with the correct files. If it is an existing database, the file may be corrupted and must be restored from the backup. Cannot open user default database. Login failed. Login failed for user "MyComputerName \ MyUserName". The log file 'C: \ Users \ MyProjectName \ App_Data \ aspnetdb_log.ldf' does not match the main file. It may be from another database, or the log may have been previously restored

The error will disappear as soon as I add ASPNETDB.MDF back to App_Data.

I searched all the code in my solution and does not reference ASPNETDB. So why is he still trying to read?

I am developing ASP.NET web forms on .Net 4.5.

+5
source share
4 answers

I had the same problem when ASPNETDB.MDF was automatically created, even if I use Asp.Net Identity as the main user management.

I solved this by removing the following line from web.config:

 <roleManager enabled="true" /> 

This ASP.NET ad uses earlier ASP.NET user membership management, which is not supported by the ASP.NET identifier.

+2
source

It seems you have something like your web.config

 <add name="DefaultConnectionForLocalDb" connectionString="Data Source=(LocalDb)\v11.0;..."; /> 

In your AccountModels.cs file you have:

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

However, it should be like this:

 <add name="DefaultConnectionForSQLEXPRESS" connectionString="data source=.\SQLEXPRESS;...;" /> public class UsersContext : DbContext { public UsersContext() : base("DefaultConnectionForSQLEXPRESS") { } } 

Then you can safely delete the DefaultConnectionForLocalDb entry from web.config and ASPNETDB.MDF with App_Data .

0
source

I don’t know if you understood it or not, but one of the things you can try: in the web.config , connections section, add <Clear/> and then <Remove Name=LocalSqlServer/>

Apparently, if you do not modify / delete LocalSqlServe , try connecting to aspnetdb.mdf.

You can also consider adding back to LocalSqlServer and point it to SqlExpress or SqlServer.

0
source

I was getting exactly the same problem. I found that VS annoyingly pulls configuration settings from machine.config, which lives outside of the project, in my case in ...

 C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config\machine.config 

Identity 2.0 used the following connection string inside machine.config ...

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

to establish a connection for ...

  <membership> <providers> <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider, ........" connectionStringName="LocalSqlServer" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="true" applicationName="/" requiresUniqueEmail="false" passwordFormat="Hashed" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="7" minRequiredNonalphanumericCharacters="1" passwordAttemptWindow="10" passwordStrengthRegularExpression=""/> </providers> </membership> 

.. (which was also installed in machine.config). If you didn’t use membership, then it’s ok to do as Lord Nick says (except for the clear / completing assignment) and just do the following in web.config ...

 <connectionStrings> <clear/> <add name="DefaultConnection" connectionString="whatever" providerName="System.Data.SqlClient" /> 

However, if you, like me, used to perform Membership functions ( https://msdn.microsoft.com/en-us/library/6e9y4s5t(v=vs.100).aspx ), you will need to comment or delete the following sections from machine.config ...

 <!-- <membership> <providers> ... </providers> </membership> <profile> <providers> ... </providers> </profile> <roleManager> <providers> .. </providers> </roleManager> --> 

... since all these things are no longer needed for AspNet Identity 2.

I also had to add the following to my web.config:

 <modules> <remove name="RoleManager"/> </modules> 

... according to this answer: The default role provider was not found on IIS 7 running on .NET 4

I hope I saved something. It took me hours and hours.

0
source

Source: https://habr.com/ru/post/1209426/


All Articles