Entity Framework basics, entityframework

Source: Internet
Author: User

Entity Framework basics, entityframework
Overview

Entity Framework (EF) is another product specifically implemented by Microsoft ORM. This blog will briefly introduce it. As for its deep-seated usage, You can query the corresponding operation manual. This article is only an entry point.


Relationship between Entity Framework and Linq to SQL

As we all know, Linq to SQL is a specific product of Microsoft's ORM ideology, and Entity Framework is also a specific product. What are the differences between the two products?

Linq to SQL is a lightweight Framework for implementation of ORM ideas, and Entity Framework is a heavyweight Framework;

One important difference between Linq to SQL and Entity Framework is that Linq to SQL can only be used for SQL Server databases, while Entity Framework can be used for many databases (such as Oracle and SQL Server ), that is to say, the Entity Framework is cross-database and should not be underestimated. In actual development, it is very cool. The following is an example.

To build a project using the classic three layers, We need to write our own D layer. If we change the database, we need to rewrite the D layer. If we use Linq to SQL when creating this project, we can easily create layer D. However, when we change the database, the Linq to SQL Framework is useless. If we use the Entity Framework when creating this project, we only need to change the configuration file, you do not need to modify the code in the program.


Why use Linq to SQL or Entity Franework?

To answer this question, let's talk about the problems in layer D.

When we write layer D, we usually abstract a SQLHelper class, which implements addition, deletion, query, and modification to the database. Then, we create the corresponding layer D Class, the functions of these methods in the D-layer class are actually the same. The only difference is SQL. From here we can see that our D-layer problems are, we repeatedly write unnecessary classes or methods. To solve this problem, we need to gradually abstract and abstract the common things, such as: able to replace with the entity set, operations on Database methods (pay attention to its parameters), and then integrate them. This is roughly the prototype of our ORM implementation. Of course, what we mentioned above, these frameworks, such as Linq to SQL or Entity Framework, have already helped us implement them. We only need to use the methods they provide. From here, we can also see that if we use these frameworks, when programming, our programmers almost don't need to manage things in layer D, but they only need to pay attention to the business implementation.


Entity Framework instance SQL script

CREATE DATABASE DBFirstGOUSE DBFirstGOCREATE TABLE T_Customer(ID INT PRIMARY KEY IDENTITY(1,1),CusName VARCHAR(20) NOT NULL)CREATE TABLE T_Order(ID INT PRIMARY KEY IDENTITY(1,1),CustomerID INT FOREIGN KEY REFERENCES T_Customer(ID),DepName VARCHAR(20) NOT NULL)

Program code





Using System; using System. collections. generic; using System. linq; using System. text; using System. data; namespace EF. entityFrameworkDemo {class Program {static void Main (string [] args) {# region add entity /// create context object // DBFirstEntities dbFirst = new DBFirstEntities (); /// operation entity // T_Customer customer = new T_Customer (); // customer. cusName = ""; // append it to the context // dbFirst. t_Customer.AddObject (customer); // save // dbFirst. saveChanges (); # endregion # region Delete entity // 1. Create a context object // DBFirstEntities dbFirst = new DBFirstEntities (); /// 2. Create an object // T_Customer customer = new T_Customer (); // customer. ID = 9; // 3. attach an object to the context for management. // dbFirst. t_Customer.Attach (customer); // modify the object status // dbFirst. objectStateManager. changeObjectState (customer, EntityState. deleted); // 4. Perform context operations on the database // dbFirst. saveChanges (); # endregion # region Modify Object // 1. Create a context object for accessing the database // DBFirstEntities dbFirst = new DBFirstEntities (); //// 2. Modify the object // T_Customer customer = new T_Customer (); // customer. ID = 4; // customer. cusName = "User 4_modified 2"; // 3. attach it to the database // dbFirst. t_Customer.AddObject (customer); // modify the object status // dbFirst. objectStateManager. changeObjectState (customer, EntityState. modified); // 4. update the database context // dbFirst. saveChanges (); # endregion # region Query DBFirstEntities dbFirst = new DBFirstEntities (); foreach (var item in dbFirst. t_Customer) {Console. writeLine (string. format ("ID: {0} Name: {1}", item. ID, item. cusName) ;}# region linq expression T_Customer cs = (from customer in dbFirst. t_Customer where customer. ID = 2 select customer ). singleOrDefault (); Console. writeLine ("ID: {0} Name: {1}", cs. ID, cs. cusName); # endregion Console. readKey ();}}}

Summary

EF can be roughly divided into DBFirst (Database-centric) and ModelFirst (entity-centric). The above is the DBFirst model. As for ModelFirst, you can search for it.


What are the advantages of Entity Framework over ADONet?

Currently, it is better to use nhib.pdf to use EntityFramework.

The advantage of Entity Framework is that it has better LINQ providers, documents, and is supported by Microsoft.

However, nhib.pdf has a large number of features not available in Entity Framework 4.0, such as batch read/write, "extra" latency, set filter, adjustment, and so on.

Fast development of ORM tools

However, I basically don't need these ORM tools. For a web application that requires high performance, high reliability, high concurrency, and distributed architecture, writing ADO. NET by myself can at least ensure data access control.

What are the advantages of Entity Framework over ADONet?

Currently, it is better to use nhib.pdf to use EntityFramework.

The advantage of Entity Framework is that it has better LINQ providers, documents, and is supported by Microsoft.

However, nhib.pdf has a large number of features not available in Entity Framework 4.0, such as batch read/write, "extra" latency, set filter, adjustment, and so on.

Fast development of ORM tools

However, I basically don't need these ORM tools. For a web application that requires high performance, high reliability, high concurrency, and distributed architecture, writing ADO. NET by myself can at least ensure data access control.

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.