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 CodeCreate 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.