ASP. MVC5 + EF6 Getting Started Tutorial (5) Model and entity Framework

Source: Internet
Author: User
Tags sql server express connectionstrings

Original: ASP. MVC5 + EF6 Introductory Tutorial (5) Model and entity Framework

Article Source: slark.net-Blog Park http://www.cnblogs.com/slark/p/mvc-5-ef-6-get-started-model.html

Previous section: ASP. NET MVC 5 Getting Started Tutorial (4) View and ViewBag

Next section: ASP. MVC5 + EF6 Tutorial (6) View Razor use

SOURCE download: Click I download

First, create model

The model in MVC is the object used to provide the view with display data.

Here we first create a model object.

In Solution Explorer, right-click the Models folder and choose Add-a class. Add a model class named Employee.cs. The Models folder is where all the model is stored by default.

Add the following code to the Employee.cs file:

namespace slarkinc.models{public    class Employee    {public        int Id {get; set;}        public string Name {get; set;}        public int Age {get; set;         }}}

So we're going to build a model. Data models contain ID, Name, age three properties.

Second, build the database

To persist the data, we use the entity Framework here.

The Entity Framework (EF) is a simple point to say. NET development of a method of accessing the database, the most important feature is the ability to access the data through an object-oriented approach without writing SQL statements.

We use the code first method of EF to create a database table.

The Code first method is simply to create an entity class in C # and the second step is to generate the corresponding database from this class.

The first step above has been completed. The second step is to create the database context before the database table is built.

Write the code in the Employee.cs file in the following form:

using  System.Data.Entity; namespace slarkinc.models{public    class Employee    {public        int Id {get; set;}        public string Name {get; set;}        public int Age {GET, set publicclass   employeedbcontext:dbcontext{  Public dbset<employee> Employees {get; set;} }}

The yellow markup part of the figure is the code added for EF persistence.

The Employeedbcontext class inherits from the DbContext provided by EF. Employeedbcontext represents the database context of the employee, and is responsible for handling data additions and deletions.

Public dbset<employee> Employees {get; set;} Map the employee class of C # to the employee table of the database.

The database we use here is SQL Server Express LocalDB. It is a lightweight free version of SQL Server. The advantage is that the VS comes with no additional installation. This allows me to upload the code directly to create the database, without additional installation and configuration. Easy to learn.

Let's configure the database link to connect to this database. Open Web. config from the Solution Explorer root directory. This is an XML configuration file. Find the <connectionStrings>...</connectionStrings> element and write its contents as follows.

<connectionStrings>    <add name= "defaultconnection" connectionstring= "Data source= (LocalDb) \v11.0; attachdbfilename=| Datadirectory|\aspnet-slarkinc-20141130103639.mdf;initial catalog=aspnet-slarkinc-20141130103639;integrated Security=true "      providername=" System.Data.SqlClient "/>    <add name=" Employeedbcontext " Connectionstring= "Data source= (LocalDB) \v11.0; attachdbfilename=| datadirectory|\employees.mdf;integrated security=true "  providername=" System.Data.SqlClient "/>   </connectionStrings>

The yellow tag in the code above is the newly added database link. There are a few things to watch out for:

    • Name= "Employeedbcontext" is the same name as the DbContext class you created. This allows DbContext to use this database link.

    • attachdbfilename=| Datadirectory|\employees.mdf; Indicates the storage location and file name of the database file. In a generic MVC5 project it should be under the App_Data folder. The file name is Employees.mdf.

Let's create the controller and view to manipulate the model.

In Solution Explorer, right-click the Controllers folder select Add Controller, select the MVC5 controller that contains the view in the pop-up selection bracket window (using the Entity Framework). Click OK.

In the Add Controller dialog box that pops up, do as configured.

    • Controller name: Employeescontroller
    • Model class: Employee (Slarkinc.models)
    • Data context: Employeedbcontext (slarkinc.models)
    • Tick the three checkboxes below
    • Click Add

The controller and view corresponding to the model class are automatically added to the project for the operation of the model for additional pruning and checking.

In Solution Explorer, right-click the Views folder under the Employees folder under the index.cshtml selection in the browser. The following results are obtained.

You can see an empty list of the model's corresponding.

Let's look at the database below. Click Show All files as shown in the small icon below Solution Explorer. Then there will be a employees.mdf file under the App_Data folder. This is the database file that EF created.

Double-click the Employees.mdf file and the Server Explorer window appears. As shown on the right. Select Data Connection->employeedbcontext-> table->employees, double-click. The main page will then display the dbo. Emplyees database table Design window. The upper section shows the table structure, and the next section shows the T-SQL statements used to create the table.

At this point, model creation and EF's operation to generate a database table using the Codefirst method are all complete.

Your message and recommendation is my motivation to write, thank you.

Previous section: ASP. NET MVC 5 Getting Started Tutorial (4) View and ViewBag

Next section: ASP. MVC5 + EF6 Tutorial (6) View Razor use

ASP. MVC5 + EF6 Getting Started Tutorial (5) Model and entity Framework

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.