EF不在MVC应用程序中创建LocalDB

时间:2016-05-19 11:36:10

标签: c# asp.net-mvc entity-framework asp.net-mvc-4

我刚刚创建了一个简单的MVC网站,只添加了一个模型:

using System;
using System.Data.Entity;

namespace MyOwnWebsiteASP4dot6.Models
{
    public class Game
    {
        public int ID { get; set; }
        public string Title { get; set; }
        public DateTime ReleaseDate { get; set; }
        public string Genre { get; set; }
    }

    public class GameDBContext : DbContext
    {
        public DbSet<Game> Games { get; set; }
    }
}

然后我添加了一个控制器并将其绑定到此模型。

我期望创建LocalDB,但是当我尝试从我的控制器访问以下方法时出现错误:

public ActionResult Index()
{
   return View(db.Games.ToList());
}

我的连接字符串:

 <add name="GameDBContext"
   connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Games.mdf;Integrated Security=True"
   providerName="System.Data.SqlClient" />

我的错误:

System.Data.SqlClient.SqlException
{"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.\r\n)"}

Windows事件日志消息:

The "DataDirectory" registry value is missing in the LocalDB instance registry key: {FB8B9694-6CD0-43DA-A73D-2727FC15C32A}

2 个答案:

答案 0 :(得分:0)

由于您为连接字符串指定了不同的名称,我相信您需要在建立连接时指定连接字符串名称。

public class GameDBContext: DbContext
{
    public GameDBContext()
      : base("GameDBContext")
    {

    }
}

答案 1 :(得分:0)

问题只是没有安装LocalDB。我发现的方法只是重新启动Visual Studio项目,然后告诉您它没有安装,并为您提供有关如何安装它的说明。可悲的是,在我实际运行它之前,我首先进行了VS的研究,调试和更新。至少现在我知道要先重新启动项目!