Entity Framework 6 Recipes translation series 1, entityframework

Source: Internet
Author: User

Entity Framework 6 Recipes translation series 1, entityframework

Microsoft's Entity Framework is gaining more and more attention and use. The Entity Framework7.0 version is coming soon. Although it is already open-source, it is a pity that there are no books on it in China, not to mention a good book, probably because the EF version is too new, no one wants to spend time translating EF books abroad. I have been using Entity Framework for more than three years, but it is quite superficial. Recently I want to learn more deeply, so I have to find the English document Entity Framework 6 Recipes. First, it should be noted that I am not good at English, just to learn EF. Write the learning process as a blog. One is to urge yourself, and the other is to help friends in need. There must be many improper translations in this article. Please correct them to avoid misleading everyone. Thank you! Let's start this learning process.

Chapter 1 use the Entity Framework

When processing relational databases, we use a table composed of rows and columns, which is highly structured and good at processing record sets. Before object-oriented programming was widely accepted, we used "procedurally" thinking and wrote structured, top-down, one-by-one functions to solve the problem. They perfectly match: in code, tables, rows, columns, and structured and procedural patterns are perfectly matched. This situation lasted for a long time.

In terms of coding, we now use object-oriented and Domain Models. architectures, designs, and coding all correspond to real-world things, such as customers and orders. We write the terms in the problem space on the whiteboard, and draw a line between them to represent Association and interaction. This serves as the basis for standardizing and assigning work to the development team. In short, the architecture, design, and coding are based on the concept layer, which is already very different from the Organization and logic of relational databases.

The methods for analyzing and solving problems in software development have evolved, but relational databases have not. For many years, data has remained in tables, rows, and columns. Unfortunately, it creates a mismatch (impedance mismatch, Microsoft's Andre) in Object-oriented Inheritance and highly standardized relational databases. hybriesberger <C # Father> may call it like this ).

To cope with this gap, the "database layer" is often introduced in the project to convert the data in the Entity class of the application field to the rows and columns in the table for storage. As a result, many commercial and developed database access frameworks are created. They all want to build a bridge between evolutionary development and structured data. Interestingly, a new solution-Object Relational ing (ORM) is generated.

The Entity Framework and the Integrated Query Language (LINQ) framework are all from Microsoft, so that we can deal with the anti-drag mismatch problem. With the Entity Framework, we can directly model domain entity classes in the designer or code. You can also establish relationships between entity classes. In the face of these entity classes and their relationships, we construct a LINQ query to deal with them. In this way, we can use entity classes and their relationships in code to express the concept of relational databases. This helps us reduce the development workload and simplify our development experience. Compared with a large number of highly redundant code ADO. NET data access methods, we use LINQ queries to express our data requirements. The object-oriented programming method replaces the highly structured relational database development method. The object framework will help you map entity classes to the underlying database.

Note: The term entity class or entity object we use is a class that represents domain items in an application. A domain class represents objects in the real world, for example, a class that represents employees, departments, and managers in your project. The end users of the application can see the domain class in the application and say, "Yes, this is what our business is doing ". The entity class defines the summary or attributes, and has no behavior. Essentially, the entity class exposes the object state.

1-1 Entity Framework

The Entity Framework is a strategic solution provided by Microsoft to implement data access for applications. It is different from previous technologies. Together with Visual Studio, the Entity Framework provides a comprehensive model-based ecosystem that allows you to develop a wide range of data-oriented applications, including desktop applications, Internet applications, and cloud applications, and service-based applications. This book will cover the vast majority of topics.

History

The Entity Framework is not a new thing. It can be traced back to Visual Studio 2008 and has gone through a long journey in terms of functions and features. 1-1:

Figure 1-1 Brief History of the object framework

The First version of the Entity Framework provides limited functions. It only provides the most basic properties of the orm and only implements a solution called "Database First, this book will fully demonstrate this solution. Version 4.0 provides a solution called "Model First", which fully supports Plain Old CLR Object (POCO) in simple public languages, and default delayed data loading behavior. Shortly afterwards, the development team of the Entity Framework released three small versions-4.1 to 4.3, providing another solution called Code First. As shown in, version 5.0 is released with. NET Framework4.5 and Visual Studio2012. It provides significant performance improvements, a series of improvements to enumeration types, table valued functions, spatial data types, stored procedures, and in-depth support for the asp.net MVC framework.

The Entity Framework has now reached version 6.0, providing asynchronous support for queries and updates. In Code First, stored procedures support updates, performance improvements, and a series of new features, this book will focus on these new features.

Note: The Entity Framework Version 5.0 can also be used in Visual Studio 2010. version 6.0 is released along with Visual Studio 2013 and is supported when running Visual Studio 2012 and Visual Studio 2010..

For level set, let's take a brief look at the key components of the Entity Framework system. But it does not mean it is a comprehensive description. It will take hundreds of pages. Let's look at some key points to help you understand the core of this book.

Model

Entity Framework is a technology that focuses heavily on modeling. When you use Entity Framework modeling, you will see many familiar symbols inherited from previous technologies and models. For example, a similar object relationship diagram and a widely used concept, logic, and physical layering method.

The model created by the Entity Framework is a model named the Entity Data Model (EDM). It allows you to use a strongly-typed entity Class During encoding, not the structure and object in a relational database. (Figure 1-2 shows the model at the concept layer). The object data model allows you to customize ing between object classes and relational database tables, it is not just a classic one-to-one or class-to-table ing.

Figure 1-2 Entity Data Model

Figure 1-2 shows how the database table on the left does not support ing to the Code on the right using the entity type. The ing capability in the object database model allows developers to replace a highly structured database with an object type set that is one to the height of the problem domain. To design high-performance, scalable, and maintainable code.

For example, Employees, Devices, and Phone Numbers are three different tables in physical storage. From the DBA's point of view, this is a perfect scenario. However, from the perspective of developers or project-related personnel, employee is a single object containing Devices and phone numbers, and a single Employee entity class is used for coding, it contains a set of Devices and Phone Numbers. Developers do not know and do not care how the database administrator stores the diagonal corner in three different database tables. Once configured, The ing between a single object and three databases will be processed by the Entity Framework.

On the contrary, a single table Department in is mapped to three specific departments. Similarly, developers and project-related personnel use a separate object to represent each department (Accounting, Marketing, Finance, and so on). However, DBA optimizes the data storage, integrate these three objects into a single database table.

Of course, you can see that in the Location table, you can easily map it to a single real break class, which is also the default behavior of the object framework.

The key point here is that developers and project-related personnel use domain entity classes in the application context, while DBA builds underlying database tables to create efficient and database. The physical framework can easily build a bridge between the two.

Layered

The Object Data Model consists of three independent layers: Concept layer, storage layer, and ing layer. Each layer is not coupled.

The object class is included in the concept layer of the object data model, which is used by developers and project-related personnel. The concept layer can be modeled by the designer and code based on how you use the Entity Framework. Once a decision is made, you can use reverse engineering to create a model from an existing database, or use the designer and a large number of tools to build a database through code modeling and using the Entity Framework. The syntax of the concept layer is defined by the conceptual Architecture Definition Language (CSDL.

Any useful application needs to persist the object to a data storage system. The data model in the Entity Framework defines tables, columns, relationships, and data types mapped to the underlying database. The Storage Architecture Definition Language (SSDL) defines the syntax of the storage model.

Finally, the ing layer defines the ing between the concept layer and the storage layer. In addition, this layer defines how object class attributes are mapped to columns in database tables. It presents the ing details window, data annotation, and code-based APIs of the object data model to developers. Its syntax is defined by the msing Specification Language (MSL.

Terms

The Entity Framework has its own vocabulary. If you have used other popular ORM tools or similar database models, you may have encountered some vocabulary before that. Although the number of complete vocabulary is huge, We only provide a few basic terms for us to start learning.

As mentioned above, an entity type represents a class in the domain model. An instance of the entity type usually refers to an entity. If you use the Entity Framework designer, an entity type is represented in the designer as a box with different attributes. Figure 1-3 shows two object types: Employee and Task.

Figure 1-3 model of one-to-multiple relationship between Employee and Task

An object type generally has one or more attributes. Like a class, an attribute is a specified value of a specific data type. The attribute can be a simple type such as integer or string, a composite type (ComplexTypes), or a set. Navigation properties refers to the properties associated with other entities (foreign key relationships in the database ). A property that is not a navigation attribute in an object type is usually called a scalar property (scalar proerties ).

The relationship between two entities is called association. The association between object types is expressed as a straight line connecting the two in the designer. The two ends of the line have the annotation that represents multiple. The Association in Figure 1-3 is a one-to-multiple association between Employeet and tasks. One Employee can have 0 or more Tasks. Each Task is associated with a specific Employee.

Each object type has an attribute or an attribute set to indicate its object key. In an object framework, an object key uniquely identifies an object. Generally, it is mapped to the primary key of the underlying database table corresponding to the object.

Finally, we did not discuss the object framework and did not mention context object. The context object is the entry to the object Framework Service. It exposes the object, manages database connections, generates parameterized SQL statements, and transfers data from the database to the database, cache objects, maintain object change tracking, and convert untyped result sets to a strongly typed collection object.

At the beginning, the context object is the ObjectContext object. Now, the object framework supports another latest context object named DbContext. DbContext greatly simplifies the experience of using the object framework. Interestingly, DbContext is a package or appearance implementer of ObjectContext. Expose the underlying ObjectContext function in an intuitive, user-friendly, and effective way.

Undoubtedly, DbContext is the first to use the object framework. At the same time, this book will introduce it in detail.

Code

Despite the strong support of the visual designer, the Entity Framework is full of code, models, entity types, associations, mappings, and other final code expressions, these codes eventually become part of the application. They can be generated by Visual Studio and the Entity Framework, or manually created by the development team. You can select some code generation tools to generate, or modify different properties in your project, or modify the underlying code generation template to generate.

Visual Studio uses a code generation technique named T4 (Text Template Transformation Toolkit) Template to automatically generate code. You can edit the T4 template in Visual Studio to generate a template suitable for the code you need. Although this is an advanced technology, we need it in many cases. We will show you how to modify it.

As an option, you can use the latest Code First technology to manually create specific Code to control the entire process. Code is preferred. developers can create entity classes, mappings, and context objects without the help of the designer. A manually created object class is generally a POCO-Plain Old CLR Objects object that does not depend on the object framework facility. More interestingly, the development team can use the powerful utility of the Entity Framework (which can be downloaded from the official Microsoft website) to reverse generate a code priority model from an existing database. Chapter 8 describes how to create object classes, mappings, and context objects before using POCO. A large number of methods throughout this book will show you how to use Code-First to solve N-layer architecture applications.

 

For details, refer to the next section. If you think it is helpful to you, please click the suggestion in the lower right to show support. Thank you. Reprinted please indicate the source: http://www.cnblogs.com/VolcanoCloud/p/4475119.html

 

  

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.