ASP. net mvc 5-create a Connection String and use SQL Server Loca

Source: Internet
Author: User
Tags sql server express

The MovieDBContext class you create is used to process the tasks that connect to the database and map the Movie object to the database records. You may ask a question, how do you specify that it will connect to the database? In fact, the database to be used is not specified, and Entity Framework uses the LocalDB for the preset value. In this section, we will explicitly add the connection string of the application in the Web. config file ).

SQL Server Express LocalDB

LocalDB is a lightweight database engine of SQL Server Express. It starts and runs in user mode. LocalDB runs in a special SQL Server Express execution mode, so you can use the MDF file database. Generally, the database files of LocalDB are stored in the App_Data folder of the web project.

Note:SQL Server Express is not recommended for Web applications in the production environment. In particular, LocalDB should not be used in the production environment of Web applications because it is not required at the beginning of design.IIS. However, the LocalDB database can be easily migrated ?? To SQL Server or SQL Azure.

Note:In Visual Studio 2013 (Visual Studio 2012), LocalDB is installed by default.

By default, Entity Framework looks like it is named as the same connection string of the object context class (such as MovieDBContext of this project. For more information, see SQL Server Connection Strings for ASP. NET Web Applications.

Open the application root directoryWeb. configFile. (NoViewFolderWeb. configFile .) Enable highlighted redWeb. configFile.

Find :

InWeb. configFile Add the following connection string.

 

The following example showsWeb. configThe newly added connection string in the file:

 
      aspnet-MvcMovie-20130603030321.mdf;Initial Catalog=aspnet-MvcMovie-20130603030321;Integrated Security=True" providerName="System.Data.SqlClient" />    
 

These two connection strings are very similar. The first connection string named DefaultConnection is used to control the member Authentication database that can access the application. The connection string you have added displays a Movie. mdf file in the App_Data folder. The database name isMovie. mdf. In this tutorial, we will not use the member database for more information about member authentication and security. For more information, see the tutorial Deploy a Secure ASP. net mvc app with Membership, OAuth, and SQL Database to a Windows Azure Web Site.

The connection string name must match the name of the DbContext class.

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
 
   Movies { get; set; }    }}
 

In fact, you do not need to add a MovieDBContext connection string. If no connection string is specified, Entity Framework creates a DbContext class for the LocalDB database in the user directory (such as MvcMovie. Models. MovieDBContext in this example ). You also name the database as anything you like, as long as it has. MDF. For example, we can name a databaseMyFilms. mdf.

Next, you will create a new MoviesController class. Can you use it ?? Displays movie data and allows you to create a new list of movies.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.