1

The error i'm getting is An exception of type

'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.

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.

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

Image of error: Image of error

The Connection String code:

    <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>

The Model Code:

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

Controller Code

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.

I have also added the Entity framework through NuGet.

Thanks in advance.

Ben
  • 21
  • 1
  • 1
  • 5
  • 5
    You need to provide the full details of the exception (including the inner exception) and where the exception is occurring (most of this code is irrelevant to your issue) –  Jan 17 '17 at 22:37
  • I have added more of the error details, is that what I should have added? – Ben Jan 17 '17 at 23:17
  • Sorry for asking, but have you installed sql server? – J. Pichardo Jan 17 '17 at 23:18
  • It means your connection string is not correct –  Jan 17 '17 at 23:22
  • **For Code Migrations** (`update-database` et al) [see this answer](https://stackoverflow.com/a/31266905/3258851). – Marc.2377 Jul 31 '18 at 21:14

2 Answers2

2

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.

So:

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

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

**Since SQL Server 2014 Express Local DB

Hth...

EdSF
  • 11,753
  • 6
  • 42
  • 83
0

Screenshot says that there is a problem between your application and sql server. Application cannot connect to the server. Possible reasons:

  1. Connection string values/format.
  2. No server or server down.
  3. Network problems.