SQL 网络接口,错误:50 - 发生本地数据库运行时错误.无法创建自动实例 [英] SQL Network Interfaces, error: 50 - Local Database Runtime error occurred. Cannot create an automatic instance

查看:45
本文介绍了SQL 网络接口,错误:50 - 发生本地数据库运行时错误.无法创建自动实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个 ASP.NET MVC 5 Web 应用程序,它在 App_Data 文件夹中有一个 MyDatabase.mdf 文件.我安装了带有 LocalDb 实例的 SQL Server 2014 Express.我可以使用服务器资源管理器编辑数据库表,但是当我调试应用程序并转到需要数据库的页面时,我收到以下错误.

I am trying to build an ASP.NET MVC 5 Web Application which has a MyDatabase.mdf file in the App_Data folder. I have SQL Server 2014 Express installed with a LocalDb instance. I can edit the database tables using the Server Explorer, however when I debug the application and go to a page where the database is needed I get the following error.

与 SQL Server 建立连接时发生与网络相关或特定于实例的错误.服务器未找到或无法访问.验证实例名称是否正确以及 SQL Server 是否配置为允许远程连接.(提供程序:SQL 网络接口,错误:50 - 发生本地数据库运行时错误.无法创建自动实例.有关错误详细信息,请参阅 Windows 应用程序事件日志.

A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 50 - Local Database Runtime error occurred. Cannot create an automatic instance. See the Windows Application event log for error details.

所以我查看了 Application 下的事件查看器,只看到一遍又一遍的警告.

So I looked in the Event Viewer under Application and only see one Warning over and over again.

指定用于缓存压缩内容的目录 C:UsersUser1AppDataLocalTempiisexpressIIS Temporary Compressed FilesClr4IntegratedAppPool 无效.正在禁用静态压缩.

The directory specified for caching compressed content C:UsersUser1AppDataLocalTempiisexpressIIS Temporary Compressed FilesClr4IntegratedAppPool is invalid. Static compression is being disabled.

所以我尝试重新启动服务器,还是不行.与之前相同的错误 50.

So I tried rebooting the server, still no go. Same error 50 as before.

我在 Models 下创建了一个类,其中我有一个名为 Post 的类.

I have created an class under Models where I have a class called Post.

namespace MyApplication.Models
{
    public class Post
    {
        public int Id { get; set; }
        public string Title { get; set; }
        public string Content { get; set; }
    }

    public class MyDatabase : DbContext
    {
        public DbSet<Post> Posts { get; set; }
    }
}

我还有一个 Controller 设置来列出来自 MyDatabase 的帖子.

I also have a Controller setup to list the posts from MyDatabase.

namespace MyApplication.Controllers
{
    public class PostsController : Controller
    {
        private MyDatabase db = new MyDatabase();

        // GET: Posts
        public ActionResult Index()
        {
            return View(db.Posts.ToList());
        }
    }

在我的 web.config 文件中,连接字符串看起来像这样...

In my web.config file the connection string looks like this...

<connectionStrings>
    <add name="DefaultConnection" 
         connectionString="Data Source=(LocalDB)v12.0;AttachDbFilename=|DataDirectory|MyDatabase.mdf;Integrated Security=True" 
         providerName="System.Data.SqlClient" />
</connectionStrings>

我已经尝试了在此处发布的建议但它没有用.也试过这个.

I've tried the suggestion posted here but it didn't work. Also tried this.

我还注意到 MyDatabase 实例在我开始运行应用程序后断开连接.如果我在 Visual Studio 中使用服务器资源管理器刷新数据库,我可以查看表.

I also notice that the MyDatabase instance gets disconnected after I start running the application. If I refresh the database using Server Explorer in Visual Studio I can view the tables.

为什么我可以连接到数据库并在 Visual Studio 2013 中对其进行编辑,但是当我调试应用程序时,它无法连接到数据库?

How is it that I can connect to the database and edit it within Visual Studio 2013 but when I debug the application it cannot connect to the database?

推荐答案

对 LocalDB 的重大更改:适用于 SQL2014;看看这篇文章,尝试使用(localdb)mssqllocaldb作为服务器名连接LocalDB自动实例,例如:

Breaking Changes to LocalDB: Applies to SQL 2014; take a look over this article and try to use (localdb)mssqllocaldb as server name to connect to the LocalDB automatic instance, for example:

<connectionStrings>
  <add name="ProductsContext" connectionString="Data Source=(localdb)mssqllocaldb; 
  ...

文章还提到了使用2012 SSMS连接2014 LocalDB.这让我相信您可能安装了多个版本的 SQL - 这让我指出 this SO answer 这表明更改 LocalDB实例"的默认名称,以避免未来可能出现的其他版本不匹配问题;提到的不是问题的根源,而是为了提高对安装在单个开发机器上的多个 SQL 版本可能导致......以及为了避免某些情况而养成的习惯的潜在冲突的认识.

The article also mentions the use of 2012 SSMS to connect to the 2014 LocalDB. Which leads me to believe that you might have multiple versions of SQL installed - which leads me to point out this SO answer that suggests changing the default name of your LocalDB "instance" to avoid other version mismatch issues that might arise going forward; mentioned not as source of issue, but to raise awareness of potential clashes that multiple SQL version installed on a single dev machine might lead to ... and something to get in the habit of in order to avoid some.

另一件值得一提的事情 - 如果您的实例由于修补它以尝试解决此问题而处于无法使用的状态,那么可能值得重新开始 - 卸载,重新安装 - 然后尝试使用 mssqllocaldb 值而不是 v12.0 并查看是否可以解决您的问题.

Another thing worth mentioning - if you've gotten your instance in an unusable state due to tinkering with it to try and fix this problem, then it might be worth starting over - uninstall, reinstall - then try using the mssqllocaldb value instead of v12.0 and see if that corrects your issue.

这篇关于SQL 网络接口,错误:50 - 发生本地数据库运行时错误.无法创建自动实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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