Entity Framework 6.0 (RECIPES) Translation Series 1-----The first chapter to start using Entity Framework 1

Source: Internet
Author: User
Tags scalar visual studio 2010

Microsoft's entity Framework is being watched and used by more and more people, and the entity Framework7.0 version is about to be released. Although it is open source, unfortunately, there is no book about it, let alone a good book, probably because the EF version of the update is too fast, no one would like to spend time translating foreign books on EF. The use of the Entity Framework has been developed for more than 3 years, but the use is very superficial, recently want to study deeply, had to find English book "Entity Framework 6 Recipes" slowly chew. The first thing to note is that my English is not good, just to learn EF. The process of learning to write a blog, one is to urge themselves, the second is to help needy friends. There must be a lot of improper translation in the text, I implore you to correct it, so as not to mislead everyone. Thank you! So much to say, let's start this learning process.

Chapter I starting with the Entity Framework

When working with relational databases, we are highly structured and adept at working with recordsets, based on tables of rows and columns. before object-oriented programming was widely accepted, we used "procedurally" thinking and solved the problem by writing a structured, top-down, one-to-one function. They correspond perfectly: In code, tables, rows, columns, and structured, procedural patterns perfectly match. Such a situation lasted for a long time.

In terms of coding, We now use object-oriented and domain models, architecture, Both design and coding correspond to things in the real world, such as customers and orders. We write the nouns in the problem domain (problem space) on the whiteboard to represent associations and interactions by drawing the lines between them. and use this as the basis for standardizing and assigning work to the development team. In summary, architecture, design, and coding are based on the conceptual layer, and there is a big difference between the organization and the logic of the relational database.

The method of analyzing and solving problems in software development has matured, but the relational database has not. For many years, data remained in the form of tables, rows, and columns. Unfortunately, it has produced a mismatch in object-oriented inheritance and highly standardized relational databases (anti-drag mismatch, micro-software Anders. Hirschberg <c# 's father > may call it that).

Entity Framework, and the Integrated Query Language (LINQ) framework, which are all from Microsoft, enable us to handle resistance mismatch issues. Using the Entity Framework, we can model domain entity classes directly in the designer or code. The relationship between entity classes can also be established. In the face of these entity classes and their relationships we build LINQ queries to deal with, and LINQ allows us to use entity classes and their relationships in code to express concepts in relational databases. These help us reduce our development effort while also helping to streamline our development experience. We use LINQ queries to express our need for data in a relatively large, highly redundant code that accesses the ADO data. Instead of a highly structured relational database development approach using object-oriented programming, the Entity Framework will help you map the entity class to the underlying database.

Note: The term entity class or entity object that we use is a class that represents a realm item in the application. A domain class represents a real-world object, such as a class that represents an employee, department, or manager in your project. The end user of the application can see the domain class in the application and say, "Yes, that's what our business does." Entity classes define profiles or attributes, no behavior, and in essence, entity classes expose the state of an object.

1-1 Entity Framework Brief

The Entity Framework is a strategic solution that Microsoft provides to enable applications to access data, different from previous technologies. The Entity Framework, together with Visual Studio, provides a comprehensive, model-based ecosystem that enables you to develop a wide range of data-oriented applications, including desktop applications, Internet applications, cloud applications, and service-based applications. This book will cover most of the topics.

History

The Entity Framework is not a new thing, it goes back to Visual Studio 2008, and it goes through a long journey on features and features. 1-1:

Figure 1-1 Brief History of the Entity Framework

The first version of the Entity Framework, which provides limited functionality, provides only the most basic physical properties of an ORM, and only implements a scheme called "Database First", which will be fully demonstrated in this book. Version 4.0 brings a scenario called Model first, complete support for simple common language runtime objects (Plain old CLR object (POCO)), and the default data lazy load behavior. Soon after, the Entity Framework's development team released three small versions-4.1 to 4.3-that provided another scenario called "Code First". As shown, version 5.0 is published with. NET Framework4.5 and Visual Studio2012. Provides significant performance improvements and supports enumeration types, table-valued functions, spatial data types, a series of improvements to stored procedures, and deep support for the ASP.

Now that the Entity Framework has reached version 6.0, providing asynchronous support for queries and updates, in code First, stored procedures that support updates, performance improvements, and a range of new features, this book will focus on these new features.

Note: Entity Framework version 5.0 can also be used in Visual Studio 2010, and version 6.0 is released with Visual Studio 20,131 and is available for Visual Studio 2012 and Visual Studio 2010 Runtime Support .

For a hierarchical set, we briefly look at the key components of the Entity Framework System. But it does not mean to be a comprehensive description, it will take hundreds of pages of space. We help you understand the core of the book by looking at some key points.

Model

The Entity Framework is a strong focus on modeling, and when you use Entity Framework modeling, you'll see a lot of familiar symbols inherited from previous techniques and patterns. For example, a similar entity diagram and widely adopted conceptual, logical, and physical layering methods.

The model created by the Entity Framework is a model called the Entity Data Model (EDM) that allows you to encode with strongly typed entity classes, not structures and objects in relational databases. (Figure 1-2 shows the model at the conceptual layer), and the Entity Data Model allows you to customize the mapping between entity classes and relational database tables, not just classic one-to-one or class-to-table mappings.

Figure 1-2 Entity Data Model

in Figure 1-2, it shows how the database table on the left does not support mapping to the right of the code using the entity type. Mapping capabilities in the entity Database model use developers to replace highly structured databases with a set of entity types that are problem to the problem domain. To design high-performance, scalable, maintainable code.

For example, the employees,devices shown in the figure above, and the phone Numbers are three different tables used in physical storage. From the perspective of the DBA (database administrator), this is a perfect scenario. However, from the perspective of a developer, or a project-related person, employee is a single object that contains devices and phone numbers, and the developer encodes a single employee entity class that contains devices and phone A collection of numbers. Developers do not know or care how the database administrator stores the diagonal in three separate database tables. Once configured, the mapping between a single object and three databases will be handled by the Entity Framework.

In the opposite case, the single-table department in is mapped to three representing a specific departments. Similarly, developers and project stakeholders use a single object to represent each department (Accounting,marketing,finance, and so on), but the DBA consolidates the three objects into a single database table for optimization of data storage.

Of course, you can see the location table in, you can easily map it to a single real break class, also this is the default behavior of the Entity Framework.

The key point here is that developers and project stakeholders use the domain entity classes that represent the context of the application, while DBAs build the underlying database tables to create efficiencies and databases. The Entity Framework makes it easy to bridge the two single bridges.

Layered

The Entity Data model consists of 3 separate layers, the conceptual layer, the storage layer, and the mapping layer. Each layer is non-coupled.

Entity classes are included in the conceptual layer of the Entity Data Model, which is used by developers and project-related personnel. Depending on how you use the Entity Framework, the concept layer can be modeled by designers and code. Once you make a decision, you can use reverse engineering to model from an existing database, or with a designer and a large number of tools to model your code, and use the Entity Framework to build a database. The syntax of the conceptual layer is defined by the conceptual schema definition language (CSDL).

Any useful application needs to persist objects to a data storage system, and the data model in the Entity Framework defines tables, columns, relationships, and data types that are mapped to the underlying database. The storage schema definition language (SSDL) defines the syntax for the storage model.

Finally, the mapping layer defines the mapping between the conceptual layer and the storage layer. In addition, this layer defines how the attributes of an entity class are mapped to columns in a database table. It is presented to developers in the Entity Data Model's Mapping Details window, data annotations, and code-based APIs. Its syntax is defined by the Mapping specification language (MSL).

Terms

The Entity Framework has its own glossary, and if you have already used other popular ORM tools or similar database models, you may have encountered some vocabulary before. While the number of complete glossaries is huge, we only provide a few basic terms that allow us to start learning.

As mentioned earlier, an entity type represents a class in the domain model. An instance of an 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 properties. Figure 1-3 shows two entity types: Employee and task.

Figure 1-3 Model for employee and task-to-many relationships

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

The relationship between two entities (relationship) is called Association (Association). An association between entity types is represented in the designer as a straight line connecting the two. Both ends of the line have annotations that represent multiplicity. The association in Figure 1-3 is an association that represents a one-to-many relationship between Employeet and a task. An employee can have 0 or more tasks. Each task is associated with a certain employee.

Each entity type has a property or a property set to indicate its entity key. In the Entity Framework, an entity key uniquely identifies an entity, and it is typically mapped to the primary key of the underlying database table for the entity.

Finally, the Entity Framework is not discussed and the context object is not mentioned. A context object is a portal to the Entity Framework service that exposes entity objects, manages database connections, generates parameterized SQL statements, Marshals (marshals) data from a database, or marshals data to a database, caches objects, maintains object change tracking, and transforms untyped result sets into a strongly typed collection object.

Initially, the context object is a ObjectContext object, and now the Entity Framework supports another new context object named DbContext. DbContext greatly simplifies the experience of using the Entity Framework. Interestingly, DbContext is a wrapper or a ObjectContext for the appearance of the player. Exposes the functionality of the underlying ObjectContext in an intuitive, friendly, and effective manner.

Undoubtedly, DbContext is already the first to use the Entity Framework. The book will also introduce it in great detail.

Code

Despite the strong support of a visual designer, the Entity Framework is represented by code, models, entity types, associations, mappings, and the final concrete code, which eventually becomes part of the application. They can be generated by Visual Studio and the Entity Framework, or manually by the development team. You can choose some code generation tools to build, either by modifying different properties in your project, or by modifying the underlying code generation template.

Visual Studio uses a code generation technique called T4 (Text template transformation Toolkit) template to generate code automatically. The T4 template in Visual Studio allows you to edit a template that generates code that is appropriate for your exact needs. Although this is an advanced technique, we need to use it in many cases. We will show you some ways to modify it.

As an option, you can control the entire process by manually creating specific code with the latest code first (Code-first) technology. With code first, developers can create entity classes, mappings, and context objects without the help of a designer. A manually created entity class, generally referred to as a simple common language runtime object (Poco-plain old CLR Objects), does not rely on Entity Framework facilities. More interestingly, the development team can use the powerful utilities of the Entity Framework (which can be downloaded from the official Microsoft Web site) to generate a code-first model in reverse from an existing database. The eighth chapter will show you the basic process of creating entity classes, mappings, and context objects before you create them using Poco. A number of ways throughout this book will show you how to use Code-first to solve N-tier architecture applications.

For the first time, learn here for the moment, for more information, please see tell. If you feel that it is helpful to you, please click on the recommendation of the lower right to demonstrate support. Thank you. Reprint Please specify source: http://www.cnblogs.com/VolcanoCloud/p/4475119.html

  

Entity Framework 6.0 (RECIPES) Translation Series 1-----The first chapter to start using Entity Framework 1

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.