.net 无法附加文件 *.mdf 作为数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17012839/
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-03 12:17:52  来源:igfitidea点击:

Cannot attach the file *.mdf as database

.netasp.net-mvcdatabaseentity-frameworkasp.net-mvc-4

提问by John Mayer

Basically I've followed a tutorial and decided to delete the .mdffile afterwards.

基本上我遵循了一个教程,然后决定删除该.mdf文件。

Now whenever I try to run the application I get the following error (the title of this thread). The code where I get the error is shown below (ASP.NET MVC 4):

现在,每当我尝试运行该应用程序时,都会出现以下错误(此线程的标题)。我收到错误的代码如下所示(ASP.NET MVC 4):

OdeToFoodDB db = new OdeToFoodDB();

public ActionResult Index()
{
    var model = db.Restaurants.ToList();
    return View(model);
}

My connection string is the following:

我的连接字符串如下:

<add name="DefaultConnection" 
     connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=OdeToFoodDb;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\OdeToFoodDb.mdf" 
     providerName="System.Data.SqlClient" />

I've tried looking at the SQL Server Object Explorer but it looks the following:

我试过查看 SQL Server 对象资源管理器,但它看起来如下:

Also, in Server Explorer I don't see any data connections.

此外,在服务器资源管理器中,我看不到任何数据连接。

And when I try to add a new connection in Server Explorer I don't see any databases named OdeToFoodDb.

当我尝试在服务器资源管理器中添加新连接时,我没有看到任何名为OdeToFoodDb.

Sorry for this wide question but I'm new to Entity Framework and don't quite get what's wrong here.

很抱歉这个广泛的问题,但我是实体框架的新手,不太明白这里有什么问题。

采纳答案by Chris

Take a look at this: Entity Framework don't create database

看看这个:实体框架不创建数据库

I would try giving the database a different name. Sometimes you can run into problems with SQL Express when trying to create a database with the same name a second time. There is a way to fix this using SQL Server Management Studio but it's generally easier to just use a different database name.

我会尝试给数据库一个不同的名称。有时,在尝试第二次创建同名数据库时,SQL Express 可能会遇到问题。有一种方法可以使用 SQL Server Management Studio 解决此问题,但通常只使用不同的数据库名称更容易。

EditThis answer was accepted because it confirms the bugand the workaround used by OP (renaming database could help). I totally agree that renaming the database is not really an acceptable way, and does not totally solve the issue. Unfortunatly I didn't check the other ways to really solve it in SSMS.

编辑此答案已被接受,因为它确认了错误和 OP 使用的解决方法(重命名数据库可能会有所帮助)。我完全同意重命名数据库并不是一种真正可以接受的方式,也不能完全解决问题。不幸的是,我没有检查在 SSMS 中真正解决它的其他方法。

回答by Davide Icardi

I think that for SQL Server Local Db you shouldn't use the Initial Catalogproperty. I suggest to use:

我认为对于 SQL Server Local Db,您不应该使用该Initial Catalog属性。我建议使用:

<add name="DefaultConnection" 
     connectionString="Data Source=(LocalDb)\v11.0;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\OdeToFoodDb.mdf" 
     providerName="System.Data.SqlClient" />

I think that local db doesn't support multiple database on the same mdf file so specify an initial catalog is not supported (or not well supported and I have some strange errors).

我认为本地数据库不支持同一个 mdf 文件上的多个数据库,因此不支持指定初始目录(或者没有得到很好的支持,我有一些奇怪的错误)。

回答by Mask

  1. From Package Manager Console run:

    sqllocaldb.exe stop v11.0

    sqllocaldb.exe delete v11.0

  2. Run your project

  3. Register a user
  1. 从包管理器控制台运行:

    sqllocaldb.exe stop v11.0

    sqllocaldb.exe delete v11.0

  2. 运行你的项目

  3. 注册用户

回答by Ahmad Moussa

Remove this line from the connection string that should do it ;) "AttachDbFilename=|DataDirectory|whateverurdatabasenameis-xxxxxxxxxx.mdf"

从应该执行此操作的连接字符串中删除此行;) "AttachDbFilename=|DataDirectory|whateverurdatabasenameis-xxxxxxxxxx.mdf"

回答by YTKim

"Cannot attach the file 'C:\Github\TestService\TestService\App_data\TestService.mdf" as database 'TestService'

“无法附加文件 'C:\Github\TestService\TestService\App_data\TestService.mdf”作为数据库 'TestService'

When you meet the above error message, Please do the following steps.

当您遇到上述错误信息时,请执行以下步骤。

  1. Open SQL Server Object Explorer
  2. Click refresh button.
  3. Expand (localdb)\MSSQLLocalDB(SQL Server 12.x.xxxxx - xxxxx\xxxx)
  4. Expand Database
  5. Please remove existed same name database
  6. Click right button and then delete
  7. Go back to your Package Manage Console
  8. Update-Database
  1. 打开 SQL Server 对象资源管理器
  2. 单击刷新按钮。
  3. 展开 (localdb)\MSSQLLocalDB(SQL Server 12.x.xxxxx - xxxxx\xxxx)
  4. 展开数据库
  5. 请删除现有的同名数据库
  6. 点击右键,然后删除
  7. 返回您的包管理控制台
  8. 更新数据库

回答by CI Apps

I have faced the same issue. The following steps in VS 2013 solved the problem for me:

我遇到了同样的问题。VS 2013 中的以下步骤为我解决了这个问题:

  1. In Server Explorer add new Connect to Database
  2. Select Microsoft SQL Server Database File as Data source
  3. Choose database filename as it should be in according to connection string in your web.config
  4. New database file was created and two database connections was appeared in Server Explorer: "MyDatabaseName" and "MyDatabaseName (MyProjectName)"
  5. Delete one connection (I've deleted "MyDatabaseName")
  1. 在服务器资源管理器中添加新的连接到数据库
  2. 选择 Microsoft SQL Server 数据库文件作为数据源
  3. 根据 web.config 中的连接字符串选择数据库文件名
  4. 创建了新的数据库文件,服务器资源管理器中出现了两个数据库连接:“MyDatabaseName”和“MyDatabaseName (MyProjectName)”
  5. 删除一个连接(我已经删除了“MyDatabaseName”)

回答by joedotnot

To fix this using SQL SERVER Management Studio

使用 SQL SERVER Management Studio 解决此问题

Your problem:You get an error such as 'Cannot attach the file 'YourDB.mdf' as database 'YourConnStringNamedContext';

您的问题:您收到错误消息,例如“无法将文件“YourDB.mdf”附加为数据库“YourConnStringNamedContext”;

Reason:happens because you deleted the backing files .mdf, ldf without actually deleting the database within the running instance of SqlLocalDb; re-running the code in VS won't help because you cannot re-create a DB with the same name (and that's why renaming works, but leaves the old phantom db name lying around).

原因:发生的原因是您删除了后台文件 .mdf、ldf,而没有实际删除 SqlLocalDb 运行实例中的数据库;在 VS 中重新运行代码无济于事,因为您无法重新创建具有相同名称的数据库(这就是重命名有效的原因,但会留下旧的幻影数据库名称)。

The Fix:I am using VS2012, adopt similarly for a different version.

修复:我使用的是 VS2012,对于不同的版本采用类似的方法。

Navigate to below path and enter

导航到下面的路径并输入

c:\program files\microsoft sql server\110\Tools\Binn>sqllocaldb info

c:\program files\microsoft sql server\110\Tools\Binn>sqllocaldb info

Above cmd shows the instance names, including 'v11.0'

上面的cmd显示实例名称,包括'v11.0'

If the instance is already running, enter at the prompt

如果实例已经在运行,在提示符下输入

sqllocaldb info v11.0

sqllocaldb 信息 v11.0

Note the following info Owner:YourPCName\Username , State:Running , Instance pipe name:np:\.\pipe\LOCALDB#12345678\tsql\query , where 123456789 is some random alphanumeric

请注意以下信息 Owner:YourPCName\Username , State:Running , Instance pipe name:np:\.\pipe\LOCALDB#12345678\tsql\query ,其中 123456789 是一些随机的字母数字

If State is not running or stopped, start the instance with

如果 State 未运行或停止,请使用以下命令启动实例

sqllocaldb start v11.0

sqllocaldb 启动 v11.0

and extract same info as above.

并提取与上述相同的信息。

In the SS Management Studio 'Connect' dialog box enter

在 SS Management Studio 的“连接”对话框中输入

server name: np:\.\pipe\LOCALDB#12345678\tsql\query

auth: Windows auth

user name: (same as Owner, it is grayed out for Win. auth.)

服务器名称:np:\.\pipe\LOCALDB#12345678\tsql\query

身份验证:Windows 身份验证

用户名:(与 Owner 相同,Win. auth 时为灰色。)

Once connected, find the phantom DB which you deleted (e.g. YourDB.mdf should have created a db named YourDB), and really delete it.

连接后,找到您删除的虚拟数据库(例如,YourDB.mdf 应该创建了一个名为 YourDB 的数据库),然后真正删除它。

Done! Once it's gone, VS EF should have no problem re-creating it.

完毕!一旦它消失了,VS EF 重新创建它应该没有问题。

回答by Stokely

You already have an old copy of that database installed in Server Explorer. So its a simple naming collision in the Server Object Explorer / SQL server. You likely created the same database Catalog Name already before you decided to move it to the Apps_Data folder. So that Database name already exists and just needs to be deleted.

您已经在服务器资源管理器中安装了该数据库的旧副本。所以它在服务器对象资源管理器/SQL 服务器中是一个简单的命名冲突。在决定将其移动到 Apps_Data 文件夹之前,您可能已经创建了相同的数据库目录名称。因此该数据库名称已经存在,只需删除即可。

Just go into Visual Studio > View > SQL Server Object Explorer and delete the old database name and its connection. Retry your app again and it should install the .mdf file in App_Data and create the same exact database again in the Server Explorer.

只需进入 Visual Studio > View > SQL Server Object Explorer 并删除旧的数据库名称及其连接。再次重试您的应用程序,它应该在 App_Data 中安装 .mdf 文件,并在服务器资源管理器中再次创建完全相同的数据库。

回答by David

I had the same error while following the tutorial on "Getting Started with ASP.NET MVC 5 | Microsoft Docs". I was on Visual Studio 2015. I opened View-> SQL Server Object Explorer and deleted the database named after the tutorial, then it could work. see Delete .mdf file from app_data causes exception cannot attach the file as database

我在学习“ASP.NET MVC 5 入门 | Microsoft Docs”教程时遇到了同样的错误。我是在Visual Studio 2015上的,打开View-> SQL Server Object Explorer,把教程命名的数据库删掉,就可以了。请参阅从 app_data 中删除 .mdf 文件导致异常无法将文件附加为数据库

回答by Steven

I found that commenting out the context section used to initialise the database resolved the problem. Havnt had time to find out what was wrong with the seeding statements yet, but removing the seeding resolved the problem.

我发现注释掉用于初始化数据库的上下文部分解决了这个问题。Havnt 有时间找出种子声明有什么问题,但删除种子解决了问题。