如何停止在 LocalDB 中使用 ASPNETDB.MDF? [英] How do I stop using ASPNETDB.MDF in LocalDB?

查看:28
本文介绍了如何停止在 LocalDB 中使用 ASPNETDB.MDF?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现了 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.config 中的 connectionString 设置为:

I have set my connectionString in web.config to:

<add name="DefaultConnection" connectionString="Data Source=MyComputerNameSQLEXPRESS;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:一个或多个文件与数据库的主文件不匹配.如果您正在尝试附加数据库,请使用正确的文件重试该操作.如果这是现有数据库,则该文件可能已损坏,应从备份中恢复.无法打开用户默认数据库.登录失败.用户MyComputerNameMyUserName"登录失败.日志文件C:UsersMyProjectNameApp_Dataaspnetdb_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 'MyComputerNameMyUserName'. Log file 'C:UsersMyProjectNameApp_Dataaspnetdb_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:WindowsMicrosoft.NETFrameworkv4.0.30319Configmachine.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>

为...建立连接

       <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 中设置).如果您还没有使用过会员资格,那么可以按照尼克勋爵的建议去做(除了明确/会完成这项工作),只需在 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/en-us/library/6e9y4s5t(v=vs.100).aspx),您需要从机器中注释掉或删除以下部分.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 上找不到默认角色提供程序

我希望我已经救了一些人.这花了我几个小时的时间来锻炼.

I hope I've saved someone some time. This took me hours and hours to work out.

这篇关于如何停止在 LocalDB 中使用 ASPNETDB.MDF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆