SQL 尝试为 .mdf 文件附加自动命名的数据库失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8747673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 13:52:14  来源:igfitidea点击:

Attempt to attach an auto-named database for .mdf file failed

asp.netsqldatabaseasp.net-mvc-3

提问by Mabbage

I am currently building a Web Application Project in Visual Studio 2010 and, until recently, have not needed to use the scaffolded code for Memberships, Users and Roles etc that are stored in the ASPNETDB.mdf database file. I have been running the project on the Visual Studio Web Server, IIS Express (from Visual studio) and also IIS7.5 without a problem.

我目前正在 Visual Studio 2010 中构建一个 Web 应用程序项目,直到最近,还不需要使用存储在 ASPNETDB.mdf 数据库文件中的成员身份、用户和角色等的脚手架代码。我一直在 Visual Studio Web Server、IIS Express(来自 Visual Studio)和 IIS7.5 上运行该项目,没有任何问题。

However, I now need to incorporate User accounts and when I use the scaffolded code to log in, I get a server error stating:

但是,我现在需要合并用户帐户,当我使用脚手架代码登录时,出现服务器错误,说明:

An attempt to attach an auto-named database for file C:\Users\User\Documents\COMP6059\PomumV2\PomumV2\App_Data\aspnetdb.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.

尝试为文件 C:\Users\User\Documents\COMP6059\PomumV2\PomumV2\App_Data\aspnetdb.mdf 附加自动命名的数据库失败。存在同名数据库,或无法打开指定文件,或位于 UNC 共享上。

This error only occurs when I am using either the IIS Express or IIS7.5 web server. The Visual Studio Web Server works fine.

此错误仅在我使用 IIS Express 或 IIS7.5 Web 服务器时发生。Visual Studio Web 服务器工作正常。

I have followed instructions from http://support.microsoft.com/?kbid=2002980to no avail.

我已按照http://support.microsoft.com/?kbid=2002980 中的说明进行操作,但无济于事。

Below is my connection string for the database:

以下是我的数据库连接字符串:

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

The error occurs on the following line of the AccountController.csfile (which is auto-generated)

错误发生在AccountController.cs文件的以下行(自动生成)

...  if (Membership.ValidateUser(model.UserName, model.Password)) { ...

I did read somewhere that I might need to move the database into SQL Server Management Studio and then attach visual studio via that, but I was unable to log into the database.

我确实在某处读到我可能需要将数据库移动到 SQL Server Management Studio,然后通过它附加 Visual Studio,但我无法登录到数据库。

I want this to be able to run on IIS7.5. Does anyone have any ideas on how to solve this issue?

我希望它能够在 IIS7.5 上运行。有没有人对如何解决这个问题有任何想法?

采纳答案by Adam Tuliper - MSFT

This is potentially an issue with the account you are running IIS under not having access to that file.

这可能是您运行 IIS 的帐户无法访问该文件的问题。

Assign full permissions to that folder for the Network Service account.

为网络服务帐户分配对该文件夹的完全权限。

You can temporarily try 'everyone' and see if it resolves the issue, and work backwards from there.

您可以暂时尝试“每个人”,看看它是否解决了问题,然后从那里向后工作。

Also ensure its not in use by the other web server (process explorer/sysinternals can help show you that)

还要确保它没有被其他 web 服务器使用(进程资源管理器/系统内部可以帮助你展示这一点)

回答by Ron Chong

check that your folder is not read-only. Works for me

检查您的文件夹是否不是只读的。对我来说有效

回答by Phill Healey

It appears the OP has resolved the issue but I though it worthwhile tagging on my solution as it may help others in the future.....

看来 OP 已经解决了这个问题,但我认为值得标记我的解决方案,因为它将来可能会帮助其他人......

I had a similar problem where the localDB went into apparent 'Read-Only' mode on the live server but worked fine on my local dev laptop.

我有一个类似的问题,localDB 在实时服务器上进入明显的“只读”模式,但在我的本地开发笔记本电脑上运行良好。

After reading numerous post like this, and trying everything I could find, it still didn't work.

在阅读了大量这样的帖子并尝试了我能找到的所有内容后,它仍然没有奏效。

By chance I went into IIS on the live server and noticed that there were 3 database connections identified. 2 were inherited -I dont know where from- and the third was locally declared(eg in the site web.config).

偶然地,我进入了实时服务器上的 IIS,并注意到识别出 3 个数据库连接。2 个是继承的 - 我不知道从哪里来 - 第三个是本地声明的(例如在站点 web.config 中)。

I deleted the 2 inherited connections and, voila! Now it works fine.

我删除了 2 个继承的连接,瞧!现在它工作正常。

回答by Kinemota

Had this issue after migration from VS2013 to VS2015 Community, while tried to open project's database .mdf file from Visual Studio 2013 folder. Then I copied project with database file to newly created Visual Studio 2015 folder and issue was solved.

从 VS2013 迁移到 VS2015 社区后遇到此问题,同时尝试从 Visual Studio 2013 文件夹打开项目的数据库 .mdf 文件。然后我将带有数据库文件的项目复制到新创建的 Visual Studio 2015 文件夹并解决了问题。