MVC series Learning (II)-First understanding of the orm framework-EF, orm-ef

Source: Internet
Author: User

MVC series Learning (II)-First understanding of the orm framework-EF, orm-ef

1. Create a console Project

2. Add a data item

Database storage model:
This model has an entity container EntityContainer,
Its Name attribute is SchoolModelStoreContainer,
The value of the EntityType attribute is Self. Student, that is, the data in <EntityType Name = "Student"> </EntityType>

Conceptual entity model:
This model has an entity container EntityContainer,
Its Name attribute is SchoolEntities,
The value of the EntityType attribute is Self. Student, that is, the data in <EntityType Name = "Student"> </EntityType>

Ing
EntityContainerMapping: specify two mapped models: SchoolModelStoreContainer and SchoolEntities,
EntitySetMpping: Specifies the entity set, that is, the conceptual entity set whose Name is Student.
EntityTypeMapping: Specifies the data type, that is, the storage entity set whose Name is Student.
MappingFragment: Specifies the ing between attributes in the object and specific columns in the table.

Private static void Add () {// 1.0 get the Student stu = new Student () {Age = 1, Name = "James 1 "}; // 2.0 "detached" the object to the packaging Class Object DbEntityEntry <Student> entry = dbContext. entry <Student> (stu); // 3.0 set the flag of the object in the packaging class to add entry. state = EntityState. added; // 4.0EF generates the corresponding SQL statement dbContext Based on the identifier of each attribute in the object. saveChanges ();}

Use SQL server profiler to monitor the SQL code generated by EF for us. The Code is as follows:

Because Added is set for all attributes of the entire packaging class, the attribute Name and Age will be inserted into the database.

Exec sp_executesql n' INSERT [dbo]. [Student] ([Name], [Age]) VALUES (@ 0, @ 1) SELECT [Id] FROM [dbo]. [Student] WHERE @ ROWCOUNT> 0 AND [Id] = scope_identity () ', n' @ 0 nvarchar (50), @ 1 int ', @ 0 = N 'zhang San 1', @ 1 = 1

Check the Code as follows:

Private static void Query () {// use a standard Query statement to find all students whose Id is 1 Student stu = dbContext. students. where (s => s. id = 1 ). select (s => s ). firstOrDefault (); Console. writeLine (stu. name + "" + stu. age + "years old ");}

Use SQL server profiler to monitor the SQL code generated by EF for us. The Code is as follows:

SELECT TOP (1)     [Extent1].[Id] AS [Id],     [Extent1].[Name] AS [Name],     [Extent1].[Age] AS [Age]    FROM [dbo].[Student] AS [Extent1]    WHERE 1 = [Extent1].[Id]

The Code is as follows:

Private static void Edit () {// 1. first, find the object Student stu = dbContext. students. where (s => s. id = 2 ). firstOrDefault (); // 2. add the object to the packaging class, and set the object packaging class ID to unchanged DbEntityEntry <Student> entry = dbContext. entry <Student> (stu); // 3. modify content stu. name = "kim"; // 3.1 set the attribute of the object to be modified to the entry. property ("Name "). isModified = true; // 4. EF generates the corresponding SQL statement dbContext Based on the identifier of the object attribute in the packaging class. saveChanges ();}

Use SQL server profiler to monitor the SQL code generated by EF for us. The Code is as follows:

When EF generates an SQL statement, the Name is inserted into the table when an SQL statement is generated because the attribute Name is changed in the packaging class.

exec sp_executesql N'UPDATE [dbo].[Student]SET [Name] = @0WHERE ([Id] = @1)',N'@0 nvarchar(50),@1 int',@0=N'kim',@1=2

The Code is as follows:

Private static void Delete () {// 1. find the object Student stu = dbContext. students. where (s => s. id = 3 ). firstOrDefault (); // 2. when an object is appended to the EF context, EF generates a packaging class object for the object and identifies each attribute as "unchanged" DbEntityEntry <Student> entry = dbContext. entry <Student> (stu); // 3. add a Deleted entry to the packaging class object. state = EntityState. deleted; // 4. when EF generates an SQL statement and the sending ID is delete, the deleted SQL statement dbContext is generated. saveChanges ();}

Use SQL server profiler to monitor the SQL code generated by EF for us. The Code is as follows:

exec sp_executesql N'DELETE [dbo].[Student] WHERE ([Id] = @0)',N'@0 int',@0=3

5. I haven't even talked about what an orm is.

Orm is Object Relational Mapping.
Chinese translation is object relationship ing,
Object: class defined in C;
Link: it can be understood as a relational database. MSSQLSERVER is one of them.
Ing: a better understanding is the one-to-one correspondence between objects and tables in the database.
EF is one of the ORM. After learning EF, it is the same as learning other ORM.

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.