Re-acquainted with the next entity Framework

Source: Internet
Author: User

What is the entity Framework

The Entity framework is an object-relational mapping O/RM framework.

The Entity Framework allows developers to manipulate relational data (relational) as if they were the domain object (domain-specific objects).

The Entity framework reduces most of the data manipulation code that you typically write.

LINQ can be used in the Entity framework to query data and to use strongly typed (strongly typed objects) to retrieve and manipulate data.

The Entity framework provides the following services that enable developers to focus more on program business logic than on the basic operation of data access.

1. Status or change tracking (changes tracking)

2. Identity or primary key recognition (identity resolution)

3. Lazy Loading (lazy loading)

4. Querying for translations (query translation)

The Entity framework is the enhancement of ADO, which provides developers with an automated mechanism for database access and storage.

The Entity framework is an open source framework.

What is O/RM

O/RM is a tool that automatically stores domain object data in relational databases, such as MS SQL Server, without requiring a large number of encodings.

The O/RM contains three important parts:

1. Domain object (Domain class objects): The class we define.

2. Relational database objects (relational databases objects): Database tables, views, stored procedures, and so on.

3. Mapping information (Mapping information): information that is converted between a domain object and a relational database object.

O/RM allows developers to independently open database design and domain object design, which makes the program more maintainable and extensible.

It also provides basic additions and deletions to the function, developers do not need to manually re-write this part of the code.

A typical database interacts with the application's o/rm as shown in the following:

  

Structure of the Entity framework

The overall structure of the Entity framework is as shown.

  

  EDM (Entity Data Model): The EDM consists of three main parts-the conceptual model (conceptual model), the storage model (Storage models), and the Mapping (Mapping).

1. Conceptual model: The conceptual model contains the class definitions for the model and the relationships between the classes. The conceptual model is designed independently of the database table design.

2. Storage Model: Storage models are database design models that contain database tables, views, stored procedures, and the relationships and keys between them.

3. Mapping: The map contains information about the conceptual model mapped to the storage model.

  LINQ to Entities: a query language written based on the object model that returns the entities that are designed in the conceptual model.

  Entity SQL: Another query language similar to the LINQ to Entities, but there are some differences, and developers need to take the time to learn it alone.

  Object Service: The primary entry for database data access, the primary responsibility is materialization (materialization), transforming the data returned by entity Client data provider into a solid object structure.

entity Client Data Provider: convert LINQ to Entities or Entity SQL to database SQL. Communicate with the ADO data provider to send or retrieve the database.

ADO Data Provider: The ADO data provider uses the standard ADO and database to interact.

The development model of the Entity framework

The Entity framework provides three development modes:

1. Code First

2. Database First

3. Model First

  

  Code First:

  

In the Code first development model, to avoid using the Visual Model Designer (EDMX), it is common to write Poco classes first and then build the database based on those classes.

Developers who follow the principle of domain-driven development (DDD) prefer to start by writing their own domain classes and then generate databases to persist data.

  Database First:

The development model for generating an EDMX (Entity Data model) from an existing database is the development model of the databases first.

If the database changes, the EDMX (Entity Data Model) is also updated. The Database first also supports stored procedures, views, and so on.

  Model First:

Model first is a compromise development pattern for code First and database first, which provides a visual model designer (EDMX) to design the data model and then builds the database and domain classes based on the database model.

  

Summarize:

1. Code first writes the domain class and then builds the database based on the class without the Visual Model Designer (EDMX).

2. Database first builds the Visual Model Designer (EDMX) and domain classes based on the databases.

3. Model first is the Visual Model Designer (EDMX), which generates the database and domain classes based on the EDMX.

Select Entity Framework Development mode

  

1. If you have a ready-made program and you have defined a domain class, you can use code First's development model to build the database for development.

2. If you have a ready-made database, you can use the development mode of the DB first to generate the EDM for development.

3. If you do not have a ready-made database and you do not have a defined domain class, and you prefer to use a graphical interface to design your database model, you can use the model first development model for development.

Personally, any scenario uses code First's development model because it is more flexible, but it is more demanding for developers themselves.

  

DbContext

  

DbContext is an important part of the Entity Framework, which is a bridge between domain or entity classes and databases.

DbContext is a very important class, and the primary responsibility is to interact with the data in the same way as the object, which contains the following activities:

  EntitySet: DbContext contains entity collections (dbset<tentity>), which map entities to database tables.

Querying: DbContext translates LINQ to Entities queries into SQL queries and sends them to the database.

Change Tracking: DbContext will track the state changes of entities queried from the database.

Persisting Data: DbContext provides database operations such as inserts, updates, and deletions based on the state of the entity.

Caching: DbContext implements a first-level cache by default, which saves the retrieved entities during the life cycle of the context class.

Manage Relationship: Database first or model first, use CSDL,MSL,SSDL to manage relationships and use the fluent API in code first to manage relationships.

Object materialization: DbContext converts the table raw data into a solid object.

  

Entity life cycle

In the lifetime of an entity, each entity has an entity state for a context-based (DbContext) operation.

An entity state is an enumeration of System.Data.Entity.EntityState types that contains the following values:

Added: The entity is being tracked by the context but does not yet exist in the database.

Deleted: The entity is being traced by the context to the database, but is marked for deletion from the database.

Modified: The entity is being tracked by the context and is in the database, and the values of some or all of the attributes of the entity have been modified.

Unchanged: The entity is being tracked by the context and is in the database, but all property values of the entity have not been modified.

Detached: The entity is not tracked by the context.

Describes how the entity state affects database operations.

1. The new entity has a added state, and dbcontext subsequent inserts are performed in the database.

2. Entities retrieved through LINQ have a state of unchanged, but if the asnotracking () method is called, its state is detached.

3. The property value of the retrieved entity is modified, and the entity modifies the status to Modified,dbcontext subsequent updates are performed in the database.

4. The entity that needs to be deleted has a deleted state, and DbContext later performs a delete operation in the database.

5. For entities already in DbContext, you can pass Dbcontext.entry (entity). state = entitystate.detached mode to set the status to detached.

Entity Framework Version

Version

Introducing Features

EF 3.5

Basic O/RM support in Database first mode.

EF 4.0

Poco support, lazy loading, testability enhancement, custom code generation, and the introduction of model first development mode.

EF 4.1

Simplified the DbContext API based on ObjectContext and introduced the Code first development model.

EF 4.3

By introducing code First migrations, you can create or modify a database from a defined code first model.

EF 5.0

Announce EF as an open source project. The introduction of enumeration support, table-valued functions, spatial data types, model multi-charts, design interface coloring shapes, bulk import of stored procedures, EF Power Tools, and various performance improvements.

EF 6.0

Introduces many new features related to code First & EF design, such as asynchronous operations (asynchronous), Elastic connections (connection resiliency), dependency resolution (dependency resolution), etc.

Note: Entity Framework core is not covered in this article.

Resources

Most of this article comes from: Entity Framework Tutorial

Small content Reference: Entity Framework official website

Re-acquainted with the next entity Framework

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.