MVC Learning Database Development pattern: Code First Instance Introduction

Source: Internet
Author: User
Tags connectionstrings

The code first in the database development model has the following main steps:

1. Create a class for the required tables in the Models folder

2. Create a data context class

3. Configure the database connection node in the Webconfig file

4. Add controller and corresponding view file

5, create the data context instance in the action of the controller, manipulate the database data through the instance

The specific steps are as follows:

1. Create a class for the required table in the Models folder

namespacecodefirst.models{ Public classBook { Public intId {Get;Set; }  Public stringName {Get;Set; }  Public stringAuthor {Get;Set; }  Public stringPrice {Get;Set; } }}

2. Create a data context class

/// <summary>    /// Create a data context     /// </summary>     Public class Booksdbcontext:dbcontext    {        publicgetset;}    }

To this, the book class under the Models folder is as follows: Note: Books is the name of the Book table in the established database.

usingSystem;usingSystem.Collections.Generic;usingSystem.Data.Entity;usingSystem.Linq;usingsystem.web;namespacecodefirst.models{ Public classBook { Public intId {Get;Set; }  Public stringName {Get;Set; }  Public stringAuthor {Get;Set; }  Public stringPrice {Get;Set; } }    /// <summary>    ///Create a data context/// </summary>     Public classMybooks:dbcontext { PublicDbset<book> Books {Get;Set; } }}

3. Configure the database connection node in the Webconfig file

<connectionStrings>    <add name=" The name of the class that inherits the data context" MyBooks"" connectionstring="Data source= Here is the server name, Initial catalog= to the database name" Books "; User id= Here is the login name of the database; password= here is
Database login Password " providername="System.Data.SqlClient"/> </ Connectionstrings>

Note: I use the Chinese character description of the place, I make changes according to the configuration of their own database can be "the Name property is the same as the data context class created above, otherwise the name of the database created is not the name of their own set, but such a string: the project name. Models. Data Context class name "

4. Add controller and corresponding view file

5, create the data context instance in the action of the controller, manipulate the database data through the instance

usingCodefirst.models;usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSYSTEM.WEB.MVC;namespacecodefirst.controllers{ Public classBookscontroller:controller {//        //GET:/books///MyBooks is the same class that inherits the DbContext data context and is also the name of the connection database string added in the Web. config file "value of the Name property"        PrivateMyBooks dbbooks =NewMyBooks ();  PublicActionResult Index () {returnView (DbBooks.books.ToList ()); }    }}

Rebuild, run results show:

Database:

After adding the data, the page shows:

Write blog, convenient and convenient for people who need!

MVC Learning Database Development pattern: Code First Instance Introduction

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.