类型'System.Data.SqlClient.SqlException'的异常-Microsoft ASP.NET MVC 5电影网站入门 [英] An exception of type 'System.Data.SqlClient.SqlException' - Microsoft Getting Started with ASP.NET MVC 5 Movie Website

查看:74
本文介绍了类型'System.Data.SqlClient.SqlException'的异常-Microsoft ASP.NET MVC 5电影网站入门的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到的错误是类型异常

The error i'm getting is An exception of type

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

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

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

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的教程.

I'm trying to complete the tutorial for the Movie App on the Microsoft MVC page.

我已经按照本教程进行了操作,但完全没有偏离,但我不断提出此错误.

I have followed the tutorial, I haven't deviated at all but I keep coming up with this error.

将提供Controller,错误图像,Web.config connectionString和Model. 如果有人可以帮助,那就太好了.

Ill supply the Controller, the error image, Web.config connectionString and the Model. If anyone can help that would be great.

错误图片: 错误图片

连接字符串代码:

    <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);
        }
    }
}

如果需要其他任何信息,请告诉我,如果我加入的内容过多,也感到抱歉,以为我应该获得更多的信息.

If anything else is needed please just let me know, also sorry if I included to much, thought I should have more info not less.

我还通过NuGet添加了Entity框架.

I have also added the Entity framework through NuGet.

谢谢.

推荐答案

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

Your EF connection string is using the "old" (since VS 2015 if I'm not mistaken**) way of connecting to localdb. You can see your DefaultConnection using the "new" way.

所以:

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

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

**自 SQL Server 2014 Express Local数据库

Hth ...

这篇关于类型'System.Data.SqlClient.SqlException'的异常-Microsoft ASP.NET MVC 5电影网站入门的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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