Entity Framework Construction of ASP. NET,

Source: Internet
Author: User

Entity Framework Construction of ASP. NET,

Some time ago, I got started with EntityFramework and got a preliminary understanding of the ORM framework. Now I want to summarize it a little.

I. Introduction to ORM

 

Object Relational ing (ORM) mode is a technology designed to solve the mismatch between object-oriented and Relational databases. To put it simply, ORM automatically persists the objects in the program to the relational database by using metadata that describes the ing between objects and databases. Don't you understand? Let's take the ORM apart for example:

O create a simple object, that is, a data Model

R relational database data table

M associates the object with the specific data table of the relational database to generate corresponding SQL operations.

In an object, features are used to identify the primary and Foreign keys, field lengths, and default values.

Ii. Install EntityFramework

Iii. Simple EntityFramework Configuration

  • Configure the database connection string in Web. config
      <connectionStrings>    <add name="DefaultConnection" connectionString="server=.;database=OnLineExamDB;uid=sa;pwd=123456;" providerName="System.Data.SqlClient" />  </connectionStrings>

    Server =.; indicates local, OnLineExamDB indicates the name of the database I connected to, and uid and pwd indicate the user password used to connect to the database.

  • To configure the Model layer and primary Key, use [Key] for identification and reference the namespace System. ComponentModel. DataAnnotations;
  • Using System; using System. collections. generic; using System. componentModel. dataAnnotations; using System. linq; using System. text; using System. threading. tasks; namespace Model {public class Class {[Key] // Add [Key] to the primary Key and reference the namespace System. componentModel. dataAnnotations; public int ClassID {get; set ;}/// <summary> // class name /// </summary> public string ClassName {get; set ;}}}
    View Code

    Create a DBFactory on the DAL data layer and inherit the DbContext to configure the relationship between the Model object and the table.

    View Code
  • Create a StudentService In The BLL service layer to query database data.

FirstOrDefault is the first element in the returned sequence. If this element is not present, the default value is returned.

  • namespace BLL{    public class StudentService    {        DBFactory dbFactory = new DBFactory();                public Student GetStudent()        {            return dbFactory.StudentFactory.FirstOrDefault();        }    }}

    Configure in Controller

    Using System; using System. collections. generic; using System. linq; using System. web; using System. web. mvc; using BLL; using Model; using OnLineExam. models; using System. web. security; namespace OnLineExam. controllers {public class UserInfoController: Controller {StudentService = new StudentService (); public ActionResult Index () {// obtain the student Model var student = StudentService. getStudent (); // save ViewData for obtaining ViewData ["Student"] = student; return View ();}}}
    View Code
  • Add View
    @ Using Model; @ {var student = ViewData ["Student"] as Student; Layout = null ;}<! DOCTYPE html> View Code

Result:

In this way, An ORM framework using EntityFramework is successfully implemented.

 

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.