The Microsoft platform has been written for a few years, with the continuous evolution of Microsoft Data access technology, I have used the ODBC, DAO, ADO, ADO, LINQ to SQL, Entity framework technologies in the program.
In recent years, the role of the Entity Framework (hereafter referred to as EF) has risen, becoming the protagonist of Microsoft's data access technology, and the data access technology I personally preferred when developing applications, and I have also seen many projects in the industry using EF on professional websites such as blog parks. I put in csdn download channel of several speaking entity Framework 5 Courseware (http://download.csdn.net/detail/bitfan/5153110), currently there are nearly 800 people downloaded, dozens of people commented, It can be inferred that a lot of people are or are going to learn ef, so they have the idea of writing a series of articles specifically for EF, focusing on my understanding of EF.
Entity Framework Series The preliminary plan is divided into the following sections, depending on the content, each part may continue to be broken down into several articles, and will be continuously modified.
A quick glance--understand entityframework
Exploring the EntityFramework--in-depth understanding of the key points of technology
Using EntityFramework in practical development
I mainly show you the charm of the EF technology from the user's point of view, but I'm not going to delve into the technical details, because it takes the whole book. The main purpose of this series of articles is to draw an EF technical navigation chart, point out its technical key points, and combined with my personal experience, give some technical solutions and suggestions for the problems that are likely to be encountered in development, and expect that the things summed up in practice will be helpful to the friends who are learning and using EF technology.
It must also be noted that EF itself is open source, but because of the time constraints, I did not take the time to read through the entire EF source code (if you really want to master EF, read through its source code is necessary), just for the scenes I find particularly useful to test, and combined with my understanding of EF to summarize and analyze, There may be a wrong place, please note for correction.
Jin Xu-liang
2013-10-16
The EntityFramework
Warm-up preparation
1 What is the Entity framework?
EF is an ORM (object-relational mapping) framework that allows us to map objects to the underlying database structure when we are programming. For example, you can create an order table in a database that maps to the order class in your program, so that each order object in the program corresponds to a record in the order table, which is responsible for converting the recordset from the database back to the object. The corresponding SQL commands can be generated according to the object's current state, and the data will be accessed (the common data access operations can be referred to as crud:create, Read, Update, Delete).
2 Why is the entity Framework used?
The EF itself is quite complex, and it takes a lot of time to learn and master it, so why do we have to spend this time?
As for my own personal experience, I think the benefits of using EF are as follows:
(1) The development efficiency of database application can be improved greatly
People based on the development of Microsoft platform, I'm afraid no one has not used ADO. Ado. NET provides a complete set of object models that are fairly consistent with the underlying database structure, easy to use, and highly efficient, before EF, the vast majority of applications are directly using ADO.
Ado. NET is really good, so what's the need to use EF?
In my opinion, in short: EF has brought more efficiency to database application development, which makes it easier to write easy-to-maintain, easy-to-scale systems, and is good enough to meet the needs of most development scenarios.
Unlike ADO, EF has a higher level of abstraction: it maps the database to DbContext, maps the data accessed in the database directly to entity objects, shields the underlying database internals, and eliminates the need to directly use the underlying objects provided by the underlying data access engine (such as the DBC provided by ADO Onnection,dbcommands, etc.) to complete the crud.
A closer look at the history of the development of software technology will reveal that software technology improves software development efficiency primarily by raising the level of abstraction . EF is the way to go, compared to ADO.
(2) As with ADO, EF is also flexible to use:
As long as you provide an EF data provider,ef you can use multiple relational databases instead of just binding to Microsoft SQL Server, for example I've used EF to store data in MySQL.
(3) EF is open:
EF itself open source, you can access: Http://entityframework.codeplex.com/SourceControl/latest download source code. Any problems can be directly read the source code to go. source code before, no secret . Open to EF brings vitality and vitality.
Ado. NET and EF are actually "squarely", ADO. NET has lower levels of abstraction, higher performance, but less development efficiency and ability to respond to change than EF, This is not to say that with the use of ADO can not be developed with high performance and easy to expand the application, but that EF is very strong in this area, directly based on the implementation of the EF is now realized by the function, it takes a lot of effort, and these features in the development of basically not open them, you do not have to make it, you have to Written by EF developers.
Another noteworthy technology is the LINQ tosql, objectively speaking, this is also an excellent technology, but the fate is indeed not good, "both born to Yoga, Sheng", it once with EF Zhengchong, but now has been limbo. Please forget about it, what LINQ to SQL can do, and EF can do. What EF can do, some LINQ to SQL does not.
3 Prerequisites for Learning EF Technology
Can anyone master EF?
No, if you want to really use EF, it is impossible not to spend a bit of effort, and some knowledge and technology do not have, it is not. Here is my summary of "learning and mastering the prerequisites of EF", if not meet any of the following, do not rush to get into the first, it is estimated that you will not find the north.
(1) How to master the basic usage of ADO and SQL Server database
Although using EF development does not need to work with the underlying database access engine and database, you still have to know their basics. For example, if you do not know how to use the DbConnection object to connect to the database, do not know how to use DbCommand to send SQL commands to the database, will not use SQL Server Management Studio, do not write SQL command, ..., then, You'd have to learn to talk about it first.
In particular, it is highly recommended to master the use of SQLProfiler, which we need to observe in the process of technical exploration to understand the SQL commands issued by EF in order to gain a deeper understanding of its characteristics.
Here's a brief look at how to install SQLProfiler. By default, the free version of SQL Express is not a tool with SQL Profiler, but we have a trickery approach:
First install SQL Serverexpress, after the completion, to the Microsoft website to download the full version of SQL Server installation package, at the time of installation Select "Add features to an existing instance", when the installation options, select "Management Tools-complete" as shown in:
Ha, now the Free Express version can also use SQL Profiler.
(2) Mastering LINQ, lambda expressions, extension methods related to data collection queries
EF supports queries written by LINQ to Entities, and we can also directly use extension methods such as FirstOrDefault (), Skip () for Ienumerable/iqueryable collection objects to complete data query work. It is commonplace to write lambda expressions in development, and friends who turn from other languages should familiarize themselves with it first.
(3) Basic knowledge of OOP programming skills and ORM.
Here are a few questions for self-testing:
- There are three main types of associations between objects: one-to-many, many-to-many, and how to implement them in C # programming?
- If you want to save objects that have the three types of associations above to the database, how should the associations between these objects be mapped to the database tables?
- In particular, if there is an inheritance relationship between objects, what should be handled?
- ......
Mastering this part of the content is very important, the actual development of a lot of problems are related to it, to find books to see it.
(4) Install Visual Studio and visit the official website
Using EF in Visual Studio is simple, create a project first, and then use NuGet to search the entity Framework:
In the event that the computer is not networked, you can add an ADO. Net. Data Model () to the project, and after completing the wizard, all related project references are set up:
I strongly recommend using the NuGet approach.
In addition, it is recommended to install EntityFramework Power Tools (http://visualstudiogallery.msdn.microsoft.com/ 72a60b14-1581-4b9b-89f2-846072eff19d), if you develop a database application using code first (described later), use this tool to parse existing code and generate a visual view of the model (read-only), It is also possible to generate all the data entities from an existing database, which is particularly interesting, and it can also generate precompiled views directly for the data model to reduce program loading time and be powerful.
Entity Frameworkpower Tools Currently the latest version is beta 4, immature, there are some functional defects, such as the existing version can not select only a few tables in the database to generate data entity classes, this tool is truly mature, stable and practical and a long way.
Finally, we introduce EF-related learning materials:
The first is the officially published technical book, in EF, the most famous is Julie Lerman (she is female) wrote three technical books, she also recorded a lot of videos on the Pluralsight website.
The official web site for EF on MSDN is:
http://msdn.microsoft.com/zh-cn/data/ee712907
This site has a detailed description of each of the EF technical points.
Another stronghold is on the ASP. NET, which is: Http://www.asp.net/entity-framework, which provides a good tutorial.
On Channel 9 (http://channel9.msdn.com) You can see some technical videos related to EF, including the latest version of EF 6.
Finally, the StackOverflow website (http://stackoverflow.com/) is the best place to solve problems in real-world development, with a number of questions and answers related to the Entity Framework that are invaluable.
The above introduction are English technical resources, E-text bad can look at the blog Park (http://www.cnblogs.com/), above a lot of domestic technical Gao wrote with EF-related blogs, a lot of valuable.
Source: >
From for notes (Wiz)
The Entity Framework Technical Guide series begins with a warm-up