【问题标题】:An exception of type 'System.Data.SqlClient.SqlException' - Microsoft Getting Started with ASP.NET MVC 5 Movie Website'System.Data.SqlClient.SqlException' 类型的异常 - Microsoft ASP.NET MVC 5 电影网站入门
【发布时间】:2017-01-17 22:35:20
【问题描述】:

我得到的错误是类型异常

“System.Data.SqlClient.SqlException”发生在 EntityFramework.dll 中,但未在用户代码中处理。

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

我正在尝试完成 Microsoft MVC 页面上的电影应用教程。

我已按照教程进行操作,我一点也没有偏离,但我一直在想这个错误。

我将提供控制器、错误图像、Web.config 连接字符串和模型。 如果有人可以提供帮助,那就太好了。

错误图片: 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 添加了实体框架。

提前致谢。

【问题讨论】:

  • 您需要提供异常的完整详细信息(包括内部异常)以及异常发生的位置(大部分代码与您的问题无关)
  • 我添加了更多的错误细节,我应该添加这些吗?
  • 不好意思问了,你安装sql server了吗?
  • 表示你的连接字符串不正确
  • 用于代码迁移update-database 等)see this answer.

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


【解决方案1】:

您的 EF 连接字符串使用“旧”(如果我没记错的话,自 VS 2015 起**)方式连接到localdb。您可以使用“新”方式查看您的DefaultConnection

所以:

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

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

**自SQL Server 2014 Express Local DB

第...

【讨论】:

    【解决方案2】:

    屏幕截图显示您的应用程序和 sql server 之间存在问题。应用程序无法连接到服务器。 可能原因:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-01
      • 2015-07-12
      • 2016-11-15
      相关资源
      最近更新 更多