无法附加文件".mdf"作为数据库"aspnet-" [英] Cannot attach the file ".mdf" as database "aspnet-"

查看:85
本文介绍了无法附加文件".mdf"作为数据库"aspnet-"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Web套接字和SqlDependency来构建游戏服务器. SqlDataReader出现错误,表明我应该调用SqlDependency.Start.我在Global.Asax中添加了以下内容:

I'm using web sockets and SqlDependency to build a game server. An error with the SqlDataReader indicated that I should call SqlDependency.Start. I included the following in my Global.Asax:

SqlDependency.Start(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);

此行始终以SqlException结尾,并显示消息:

This line always ends with the SqlException, with message:

Cannot attach the file 'C:...aspnet-ProjectName-11111111111.mdf' as database 'aspen-ProjectName-11111111111'.

我已经尝试修复了两天.我已经启动了一个新的MVC 4 WebAPI应用程序,该应用程序具有基本的模型,上下文和种子,无法解决该错误.我在以下尝试了各种解决方案:

I've been trying to fix this for two days. I've started a fresh MVC 4 WebAPI app, with a basic model, context, and seed, and can't get around this error. I've tried the various solutions in the following:

ASP.NET MVC4代码首先-无法将文件作为数据库附加"例外

EF5:无法附加文件"{0}"作为数据库'{1}'

没有变化.我正在Visual Studio 2012中运行MVC4 API,SQL Server是2014.

No change. I'm running MVC4 API in Visual Studio 2012, SQL Server is 2014.

这是数据库连接问题,对吗?我的AppData文件夹中的.mdf文件(两个项目中都存在该文件和日志文件)无法连接到SQL Server?另外,有帮助吗?

This is a DB connection problem, right? The .mdf file in my AppData folder (both it and the log file are there in both projects) can't be connected to SQL Server? Also, help?

推荐答案

我遇到了与您相同的问题.

I encountered the same problem as you.

在Web.config文件中,找到您的连接字符串,将其复制并粘贴,然后删除"MultipleActiveResultSets"之后的所有内容-除了providerName.

In your Web.config file, find you connection string, copy and paste it and then remove everything after the 'MultipleActiveResultSets' - apart from the providerName.

所以在我看来,它是这样的:

So in mine, it changed from this:

<add name="ApplicationName" connectionString="Data Source=(localdb)\MSSQLLocalDB; Initial Catalog=ApplicationNameContext-20151023111236; Integrated Security=True; MultipleActiveResultSets=True; AttachDbFilename=|DataDirectory|ApplicationNameContext-20151023111236.mdf" providerName="System.Data.SqlClient" />

对此表示敬意:

<add name="NotificationConnection" connectionString="Data Source=(localdb)\MSSQLLocalDB; Initial Catalog=ApplicationNameContext-20151023111236; Integrated Security=True;" providerName="System.Data.SqlClient" />

您会注意到该连接具有不同的名称. 连接仍将查询相同的数据库.

And as you will notice the connection has a different name. The connections will still query the same database.

现在将Dependency.Start参数中的连接字符串名称修改为您刚创建的连接字符串的名称:

Now modify your connection string name in the Dependency.Start parameter to be your the name of the connection string you just created:

SqlDependency.Start(ConfigurationManager.ConnectionStrings["NEW_CONNECTION_NAME"].ConnectionString);

这篇关于无法附加文件".mdf"作为数据库"aspnet-"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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