2017-01-17 20 views
0

,我发现了错误类型的异常型“System.Data.SqlClient.SqlException”例外 - 微软开始使用ASP.NET MVC 5电影网站

'System.Data.SqlClient.SqlException' occurred in EntityFramework.dll but was not handled in user code.

Additional information: 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.

我想在Microsoft MVC页面上完成Movie App的教程。

我按照教程,我没有偏差,但我一直在提出这个错误。

Ill提供Controller,错误图像,Web.config connectionString和Model。 如果任何人都可以提供帮助,那会很棒。

错误的图像: Image of error

连接字符串的代码:

<connectionStrings> 
     <add name="DefaultConnection" connectionString="Data Source= (LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-MvcMovie-20170117075430.mdf;Initial Catalog=aspnet-MvcMovie-20170117075430;Integrated Security=True" providerName="System.Data.SqlClient" /> 
    <add name="MovieDBContext" 
    connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Movie.mdf;Integrated Security=True" 
    providerName="System.Data.SqlClient" 
/> 
    </connectionStrings> 

模型代码:

using System; 
using System.Data.Entity; 

namespace MvcMovie.Models 
{ 
    public class Movie 
    { 
     public int ID { get; set; } 
     public string Title { get; set; } 
     public DateTime ReleaseDate { get; set; } 
     public string Genre { get; set; } 
     public decimal Price { get; set; } 
    } 

    public class MovieDBContext : DbContext 
    { 
     public DbSet<Movie> Movies { get; set; } 
    } 
} 

控制器代码

using System; 
using System.Collections.Generic; 
using System.Data; 
using System.Data.Entity; 
using System.Linq; 
using System.Net; 
using System.Web; 
using System.Web.Mvc; 
using MvcMovie.Models; 

namespace MvcMovie.Controllers 
{ 
    public class MoviesController : Controller 
    { 
     private MovieDBContext db = new MovieDBContext(); 

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

     } 

     // GET: Movies/Details/5 
     public ActionResult Details(int? id) 
     { 
      if (id == null) 
      { 
       return new HttpStatusCodeResult(HttpStatusCode.BadRequest); 
      } 
      Movie movie = db.Movies.Find(id); 
      if (movie == null) 
      { 
       return HttpNotFound(); 
      } 
      return View(movie); 
     } 

     // GET: Movies/Create 
     public ActionResult Create() 
     { 
      return View(); 
     } 

     // POST: Movies/Create 
     // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
     // more details see http://go.microsoft.com/fwlink/?LinkId=317598. 
     [HttpPost] 
     [ValidateAntiForgeryToken] 
     public ActionResult Create([Bind(Include = "ID,Title,ReleaseDate,Genre,Price")] Movie movie) 
     { 
      if (ModelState.IsValid) 
      { 
       db.Movies.Add(movie); 
       db.SaveChanges(); 
       return RedirectToAction("Index"); 
      } 

      return View(movie); 
     } 

     // GET: Movies/Edit/5 
     public ActionResult Edit(int? id) 
     { 
      if (id == null) 
      { 
       return new HttpStatusCodeResult(HttpStatusCode.BadRequest); 
      } 
      Movie movie = db.Movies.Find(id); 
      if (movie == null) 
      { 
       return HttpNotFound(); 
      } 
      return View(movie); 
     } 

     // POST: Movies/Edit/5 
     // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
     // more details see http://go.microsoft.com/fwlink/?LinkId=317598. 
     [HttpPost] 
     [ValidateAntiForgeryToken] 
     public ActionResult Edit([Bind(Include = "ID,Title,ReleaseDate,Genre,Price")] Movie movie) 
     { 
      if (ModelState.IsValid) 
      { 
       db.Entry(movie).State = EntityState.Modified; 
       db.SaveChanges(); 
       return RedirectToAction("Index"); 
      } 
      return View(movie); 
     } 

     // GET: Movies/Delete/5 
     public ActionResult Delete(int? id) 
     { 
      if (id == null) 
      { 
       return new HttpStatusCodeResult(HttpStatusCode.BadRequest); 
      } 
      Movie movie = db.Movies.Find(id); 
      if (movie == null) 
      { 
       return HttpNotFound(); 
      } 
      return View(movie); 
     } 

     // POST: Movies/Delete/5 
     [HttpPost, ActionName("Delete")] 
     [ValidateAntiForgeryToken] 
     public ActionResult DeleteConfirmed(int id) 
     { 
      Movie movie = db.Movies.Find(id); 
      db.Movies.Remove(movie); 
      db.SaveChanges(); 
      return RedirectToAction("Index"); 
     } 

     protected override void Dispose(bool disposing) 
     { 
      if (disposing) 
      { 
       db.Dispose(); 
      } 
      base.Dispose(disposing); 
     } 
    } 
} 

如果需要其他东西,请让我知道,如果我包括了很多,也对不起,我认为我应该有更多的信息不少。

我也通过NuGet添加了实体框架。

在此先感谢。

+4

您需要提供异常的全部细节(包括内部异常)以及发生异常的位置(大部分代码与您的问题无关) –

+0

我已经添加了更多的错误细节,是我应该添加什么? – Ben

+0

对不起,但是你安装了sql server吗? –

回答

1

您的EF连接字符串正在使用连接到localdb的“旧”(因为VS 2015如果我没有弄错**)的方式。您可以使用“新”方式查看您的DefaultConnection

所以:

<add name="MovieDBContext" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;...` 

不:Data Source=(LocalDB)\v11.0...

**由于SQL Server 2014 Express Local DB

H个...

+0

工作正常!非常感谢你... – Ben

+0

@Ben酷豆:) – EdSF

0

截图说有你的应用程序和SQL Server之间的问题。应用程序无法连接到服务器。 可能的原因:

  1. 连接字符串值/格式。
  2. 没有服务器或服务器停机。
  3. 网络问题。