Getting started with ASP.NET MVC - using database (SQL server)

Create a connection string and use SQL Server LocalDB

MovieDBContext the class you create will handle the task of connecting to the database and map the Movie object to the database record. But one question you might ask is how to specify the database to which it will connect. In fact, it is not necessary to specify the database to use, and the Entity Framework will default to use LocalDB . In this section, we will explicitly add a connection string to the Web.config file of the application.

SQL Server Express LocalDB

LocalDB Is a lightweight version of the SQL Server Express database engine that starts on demand and runs in user mode. LocalDB runs in the special execution mode of SQL Server Express, which enables you to use the database as an. mdf file. Typically, the LocalDB database file is saved in the application of the web project_ Data folder.

SQL Server Express is not recommended for production web applications. LocalDB should not be used in production with web applications because it cannot be used with IIS. However, you can easily migrate the LocalDB database to SQL server or SQL Azure.

In Visual Studio 2017, LocalDB is installed using visual studio by default.

By default, the Entity Framework looks for the object context class (the same connection string MovieDBContext) with the same name as this project.

Open the application root Web.config file shown below. (not the Web.config file in the Views folder.)

Find the < connectionstrings > element:

 

Add the following connection strings to the elements in the < connectionstrings > web.config file.

<add name="MovieDBContext" 
   connectionString="Data Source=(LocalDb)\MSSQLLocalDB;Initial Catalog=aspnet-MvcMovie;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\Movies.mdf" 
   providerName="System.Data.SqlClient" 
/>

 

The following example shows a portion of the Web.config file with a new connection string added:

<connectionStrings>
  <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;Initial Catalog=aspnet-MvcMovie-fefdc1f0-bd81-4ce9-b712-93a062e01031;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-MvcMovie-fefdc1f0-bd81-4ce9-b712-93a062e01031.mdf" providerName="System.Data.SqlClient" />
  <add name="MovieDBContext" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;Initial Catalog=aspnet-MvcMovie;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\Movies.mdf" providerName="System.Data.SqlClient" />
</connectionStrings>

The two connection strings are very similar. First name the first connection string DefaultConnection and use it in the membership database to control who can access the application. The added connection string specifies the LocalDB database named movie. MDF located in the application data folder. We will not use the membership database in this tutorial , for more information about membership, authentication, and security

The name of the connection string must be the same as DbContext The name of the class does not match.

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

In fact, there is no need to add MovieDBContext connection string. If the connection string is not specified, the Entity Framework will create a LocalDB database in the user directory, which has DbContext The fully qualified name of the class (in this case, MvcMovie.Models.MovieDBContext). As long as the database has, it can be named with any name.. MDF suffix. For example, we can name the database MyFilms.

Next, you will generate a new MoviesController class that you can use to display movie data and allow users to create new movie listings.

Keywords: C# SQL Server Back-end mvc

Added by tjohnson_nb on Sun, 28 Nov 2021 19:56:59 +0200