Learn ASP (iv)-my first ASP. NET MVC entity Object

Source: Internet
Author: User
Tags sql server connection string connectionstrings

Today I will add some classes based on the table structure in the database. These classes will become part of the "model" in this ASP. NET MVC application.
We use the Entity Framework to define and use these model classes and to access the database. The Entity Framework (EF) is an object-relational mapping mechanism that enables. NET developers to work with relational data using specific objects. It eliminates the need for developers to write most of the data access code, so it is also called code-first development mode. With the Entity Framework, you can use a custom data class with a data model without any modifications to the data class itself. This means that the "pure legacy" CLR object (POCO) can be used with the data model. These POCO data classes (also known as "persistent unknown objects") map to entities defined in the data model, and they support most query, insert, UPDATE, and delete behaviors that are identical to the entity types generated by the Entity Data Model tools.

I. Adding an entity class


in Visual Studio, in Solution Explorer, right-click the Models folder and select Add-and-class. Such as.

In the pop-up dialog box, enter "book" for the class name. Add the following seven properties to the book class:

    Public classBook { Public intBookID {Get;Set; }  Public stringCategory {Get;Set; }  Public stringName {Get;Set; }  Public intnumberofcopies {Get;Set; }  Public intAuthorid {Get;Set; }  Public decimalPrice {Get;Set; }  PublicDateTime Publishdate {Get;Set; } }

I use the book class to represent a row of records in the books table in the database. Each instance of the book object will correspond to a row of data in the database table (Books), and each property of the book class will be mapped to individual columns in the table.
In the same file, add the Bookdbcontext class with the following code:

 Public class Bookdbcontext:dbcontext    {        publicgetset;}    }

This Bookdbcontext class represents the contents of the Book table (Books) of the Entity Framework, which adds, modifies, deletes, and queries the Books tables in the database. This bookdbcontext inherits from the DbContext base class of the Entity Framework.
To be able to use the DbContext and Dbset two classes, you need to add the following statement at the top of the file:

Using System.Data.Entity;

The following full Book.cs file is shown below.

usingSystem;usingSystem.Data.Entity;namespacemvcmovie.models{ Public classMovie { Public intID {Get;Set; }  Public stringTitle {Get;Set; }  PublicDateTime ReleaseDate {Get;Set; }  Public stringGenre {Get;Set; }  Public decimalPrice {Get;Set; } }      Public classMoviedbcontext:dbcontext { PublicDbset<movie> Movies {Get;Set; } }} 

Ii. creating a SQL Server connection string

The main function of the Bookdbcontext class you just created is to connect the database and map database table records to implement Curd. How do I connect to the specified database? By increasing the database connection information in the Web. config file.
First, open the Web. config file under the application's root directory. Such as.

The <connectionStrings> element in the Web. config file will have a connection by default, such as.

What we're going to do is to add the following connection string to the <connectionStrings> element in the Web. config file

<name= "Bookconnection"  connectionString= "Data source=.\ Sqlexpress;initial catalog=test;integrated Security=sspi "
ProviderName= "System.Data.SqlClient"/>

A part of the Web. config file that adds a new connection string, such as.

As long as you write a small amount of code and write the configuration information in the Web. config file, all the functional code for the database curd operation is completed.

In the next article, I'll create a list page for displaying book information, as well as the Bookcontroller class for new, modified, deleted, and queried operations.

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.