如何停止在LocalDB中使用ASPNETDB.MDF? [英] How do I stop using ASPNETDB.MDF in LocalDB?
问题描述
我实现了ASP.NET Identity,它会在我的App_Data文件夹中自动创建ASPNETDB.MDF和aspnetdb_log.ldf。我的SQL Express实例中已经有AspNet表(即AspNetRoles,AspNetUsers等)(这是我所有其他表所在的位置)。据我所知,我的应用程序正在从SQL Express数据库而不是ASPNETDB.MDF中读取成员身份和角色数据。
I implemented ASP.NET Identity and it automatically created ASPNETDB.MDF and aspnetdb_log.ldf in my App_Data folder. I already have the AspNet tables (i.e., AspNetRoles, AspNetUsers, etc) in my SQL Express instance (which is where all my other tables are sitting). As far as I can see, my application is reading and writing membership and role data from the SQL Express database and not ASPNETDB.MDF.
我已经在web中设置了我的connectionString .config到:
I have set my connectionString in web.config to:
<add name="DefaultConnection" connectionString="Data Source=MyComputerName\SQLEXPRESS;Initial Catalog=MyDatabaseName;Integrated Security=True" providerName="System.Data.SqlClient" />
但是,如果我从App_Data中删除ASPNETDB.MDF,则在登录时会出现以下错误:
However, if I remove ASPNETDB.MDF from App_Data, I get the following error when I login:
异常详细信息:System.Data.SqlClient.SqlException:一个或多个文件与数据库的主文件不匹配。如果您尝试附加数据库,请使用正确的文件重试该操作。如果这是现有数据库,则该文件可能已损坏,应该从备份中还原。
无法打开用户默认数据库。登录失败。用户 MyComputerName\MyUserName的
登录失败。
日志文件 C:\Users\MyProjectName\App_Data\aspnetdb_log.ldf与主文件不匹配。它可能来自其他数据库,或者以前可能已经重建了日志
Exception Details: System.Data.SqlClient.SqlException: One or more files do not match the primary file of the database. If you are attempting to attach a database, retry the operation with the correct files. If this is an existing database, the file may be corrupted and should be restored from a backup. Cannot open user default database. Login failed. Login failed for user 'MyComputerName\MyUserName'. Log file 'C:\Users\MyProjectName\App_Data\aspnetdb_log.ldf' does not match the primary file. It may be from a different database or the log may have been rebuilt previously
将ASPNETDB.MDF添加回App_Data后,错误就消失了。
The error goes away once I add ASPNETDB.MDF back to App_Data.
我已经搜索了解决方案中的所有代码,但没有引用ASPNETDB。那么,为什么仍要尝试从中读取内容呢?
I have searched all the code in my solution and it makes no reference to ASPNETDB. So why is it still trying to read from it?
我正在.Net 4.5上开发ASP.NET Web表单。
I am developing ASP.NET web forms on .Net 4.5.
推荐答案
我遇到了完全相同的问题。我发现VS烦人地从位于项目外部的machine.config中提取配置设置,以我为例...
I was getting exactly the same problem. I discovered that VS annoyingly pulls in config 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在machine.config中使用了以下连接字符串...
Identity 2.0 had 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 set up 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>
..(也已在machine.config中设置)。如果您还没有使用过Membership,那么可以按照Nick勋爵的建议进行操作(除了clear /会做这件事),只需在web.config中执行以下操作即可。
.. (which was also set in machine.config). If you haven't been using Membership then it's fine to do as Lord nick suggests (except just the clear/ will do the job) and simply do the following in web.config...
<connectionStrings>
<clear/>
<add name="DefaultConnection" connectionString="whatever" providerName="System.Data.SqlClient" />
但是,如果您喜欢我,以前有会员资格正在运行( https:// msdn.microsoft.com/zh-cn/library/6e9y4s5t(v=vs.100).aspx ),则需要注释掉或从machine.config中删除以下部分...
However, if you, like me, previously had Membership running (https://msdn.microsoft.com/en-us/library/6e9y4s5t(v=vs.100).aspx), you will need to comment out or delete the following sections from machine.config...
<!--
<membership>
<providers>
...
</providers>
</membership>
<profile>
<providers>
...
</providers>
</profile>
<roleManager>
<providers>
..
</providers>
</roleManager>
-->
...因为AspNet Identity 2不再需要所有这些东西。
... since all this stuff is no longer needed for AspNet Identity 2.
我还必须在web.config中添加以下内容:
I also had to add the following into in my web.config:
<modules>
<remove name="RoleManager"/>
</modules>
...按照以下答案:在运行.NET 4的IIS 7上找不到默认角色提供程序
... as per this answer: Default Role Provider could not be found on IIS 7 running .NET 4
我希望我已经节省了一些时间。这花费了我几个小时的时间。
I hope I've saved someone some time. This took me hours and hours to work out.
这篇关于如何停止在LocalDB中使用ASPNETDB.MDF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!