You should be familiar with this figure.
This is the structural diagram of Microsoft's object data model for data access.
Why is another data model required?
So why do we need to build another model? As the company's data processing volume increases, streamline the data relationship and develop applications based on the data.ProgramIt becomes very difficult. Storage problems (such as data integrity, performance, and management) need to be considered in the design of the database architecture. Sometimes this is not easy to understand. These architectures often conflict with application structures, making development and maintenance more complex.
We often encounter a custom solution that separates the data structure from the built application. Unfortunately, for each application, the number of custom solutions, a variety of methods, and the steps required for modeling data vary, resulting in problems constantly occurring. The whole industry hopes to have a way to define and develop application-level domain models so that they can be clearly separated from the storage of logical models. Therefore, the Entity Framework is introduced.
EDM allows you to define a domain model by means of Data Organization (not data storage. Another major goal of EDM development is to become a core data model for developers and server technology suites in Microsoft.
By using a core data model, application maintenance is simplified. After achieving this goal, EDM can play a role, not only based on ADO. A custom application-defined model built by the net entity framework can also be used as input for reporting and visualization applications, Intranet portal applications, or workflow applications.
Similar to the ER model, EDM uses two main concepts: Entities (things) and Their Relationships (or associations ). When considering the storage of entities and associated instances (or closed packages operated by these instance sets), you also need to use the concept of "set. Therefore, more clearly, entities exist in entityset and associations exist in associationset.
The last schema concept defined in EDM is the entitycontainer concept, which can define a closed package around the instance and link set described earlier. These simple concepts allow developers to define a domain model that can be mapped back to the persistence layer and the classes used in the application itself. (It should be noted that the persistence layer of EDM does not need to be related, although it is related now .)
Each entity type defined in EDM can contain two different types of members. One is used to define the attributes of an object (similar to columns in a database), and the other is the navigation attribute, it supports navigation relationships involving entity types (usually expressed as foreign keys in the database ). In addition, each object type must have a unique identifier or keyword.
Why?XMLTo describeEDM?
After careful consideration, XML is selected as the first sequence representation of EDM. Load data by generating an XML file (or resource) or dynamically generated XML Representation, developers and third parties can use well-defined XML formats to convert such formats and load them to the metadata runtime of the object framework. However, other representations of EDM creation are completely possible, and there may be other alternative representations in future versions as the product develops.
The current EDM syntax is defined using the XML Schema Definition Language (XSD) attached to the product. However, most people should not be expected to develop XML manually, but should think that people will use the tools provided in Visual Studio. That is to say, the Team is aware of the attention of the domain-specific language (DSL) and the standby persistence mechanism of the edm model (usually for databases, these options are also being evaluated for extension in the forthcoming version.
Who needs another new query language?
The last question about EDM development is why we need to create a new query language? Why not use the existing language? After studying an EDM in depth, the answer becomes clearer.
So far, I have introduced why to create EDM and various structures used in EDM, and the fact that this model is a descendant of the object link model. In order to allow the created model to be clearly mapped to the underlying data storage, it can also represent the application-level domain model that developers use when programming, EDM needs to be able to model various concepts (such as inheritance and polymorphism. Because the current relational query language does not support query based on the returned values of inheritance, relational navigation, or polymorphism results, a new query language needs to be developed to meet this requirement.
This gave birth to the Entity SQL (esql), which is a new SQL language, which includes the concept-based query function not supported by the previous SQL language. Esql extends the existing SQL language in a similar way as the relational model used in the EDM extension database. In addition, esql is not bound to any background database-specific syntax, so you can write queries (and/or applications) at one time, no matter which background database you are targeting. The following uses a simple esql query as an example to describe how to retrieve a blog with at least one post and related personnel (in my model, it refers to the blog owner) from all the blogs:
Select C, C. person from travelentitiesgeneral. blogs as c Where C. blogposts. Count> 0
ImplementationEDM
ADO. NET Entity Framework evolved from ADO. NET and is the first specific implementation of EDM. It can provide high-level abstraction when developing relational databases. In Version 1.0, the team has been focusing on building the Platform Foundation, not just a simple Orm, which allows developers to use very flexible ing to process conceptual models or object models, and can adapt to the existence of huge differences with the underlying storage.
This high degree of flexibility and the enormous difference with the underlying storage are the key to allowing separate development of databases and applications. When the database architecture changes, the application does not need to be modified because it adopts the Entity Framework, And you usually do not need to rewrite the application content, update the ing file as necessary to adapt to this change.
To start developing ADO.. NET platform. NET 2.0 Provider Model is the basis for building the Entity Framework, and the existing provider is updated as appropriate to support the new entity framework and ADO.. Net 3.5. We chose to implement it based on the existing ADO. net provider model to ensure developmentCommunityNo stranger to the provider model.
Architecture 1. You will notice that acceptable architectures include the conceptual Architecture Definition Language (CSDL), ing architecture language, and Storage Architecture Definition Language (SSDL ). You will also notice that the Entity Framework includes the updated sqlclient data provider that supports the canonicalized command tree (CCT.
Figure 1 architecture of ADO. NET Entity Framework
Entityclient
Then, the Entity Framework introduces the new ADO. net provider entityclient based on these ADO. Net 3.5 providers. Entityclient looks very similar to the previously used ADO. net provider. It will provide the first abstraction that allows developers to use standard connection, command, and datareader objects to perform queries based on EDM. It also adds the client view engine (defined by EDM) required for the ing domain model to the underlying relational database architecture. If necessary, entityclient can use esql query strings to allow developers to process entities in rows and columns without generating classes to represent the conceptual architecture.
In the first phase, the command tree is still represented by EDM. The client view engine draws on the specific view theory in the database system and applies these theories to the data access layer. It applies a ing conversion in the tree, you can create a tree that represents the same operation in the underlying logical storage model and delete any non-relational concepts (such as relationships, inheritance, and polymorphism ). The newly converted tree is passed to ADO. net 3.5 provides the program service, and this service will return the dbcommand command that encapsulates the local SQL statement stored at the underlying layer, and then the command is executed, and the result is returned through the stack up.
You can use multiple methods to define mappings used in the client view engine for conversion between EDM and the logical database architecture. This ing can be specified using the msing standard language (MSL), which is a declarative XML syntax, you can create and edit an object by writing XML manually or using the entity ing tool included in Visual Studio.
During compilation, MSL allows the Entity Framework to generate necessary queries and Update views. These views are then used in the client view engine to complete the query (defined by EDM) to the logical Storage Architecture.
Another way to express ing or partial ing is to use esql queries. In this case, when developers use esql to express the query view, the infrastructure requires them to define the accompanied create, update, and delete mappings in the ing specification. This operation is required, because if you can use the esql function in the query view, you can define a view for a query that does not have a valid update view, the ing infrastructure cannot generate the corresponding update view for the query.
ExampleCode
StringCustomerid = txtcustomerid. Text. Trim ();
// Contains a reference to an Entity Data Model (EDM) and a data source connection. Using(EntityconnectionCN =New Entityconnection("Name = northwindentities")) { CN. open (); EntitycommandCmd = cn. createcommand (); Cmd. commandtext = "Select value c from northwindentities. MERs"+ "As c Where C. customerid = @ customerid"; Cmd. Parameters. addwithvalue ("Customerid", Customerid ); DbdatareaderRDR = cmd. executereader (Commandbehavior. Sequentialaccess ); While(RDR. Read ()) Console. Writeline (RDR ["CompanyName"]. Tostring ()); RDR. Close (); }
Object Service
Based on the entityclient provider, the Entity Framework adds another set of abstractions to allow development of untyped data records returned by objects rather than entityclient. This is usually considered an ORM layer. It can generate CLR instances of the types defined in the data model and allow developers to use LINQ or esql to query these objects. It also happens to be the Entity Framework layer that attracts the most attention of many developers looking for available ORM technologies in the market.
The advanced function of the object service layer is to accept esql or LINQ queries from the application, pass the query expression to the entityclient below, and return ienumerable <t>. However, after in-depth analysis, you will find that the center of the object service layer is the objectcontext that represents the interaction session between the application and the underlying data storage.
Objectcontext is the main structure used by developers to query, add, and delete entity instances, and save New States back to the database.
If you use object service, the process for developers to track changes to objects in the memory and to save these changes back to the database is simplified. Object Service uses objectstatemanager to track not only the current status of instances in the memory, but also the initial status of each instance retrieved from the repository, this allows the Entity Framework to apply the optimal concurrent operations when pushing data back to the database. By calling the savechanges Method for objectcontext, you can easily save the tracked changes and push them back to the data repository.
So far, I have been talking about the basic content about objectcontext and using some examples to introduce the basic usage of objectcontext, which is usually used when dynamic tools or applications need to use the EDM model. However, when Visual Studio is used as the development environment, Developers find that the strongly typed objectcontext also has the advantage of adding attributes and methods to the surface features that may be specific to the target EDM.
Sample Code:
NorthwindentitiesNorthwindcontext =New Northwindentities(); StringCustomerid = txtcustomerid. Text. Trim (); Objectquery<MERs> Query = northwindcontext. createquery <MERs> ( "Select value c from MERs as c Where C. customerid = @ customerid", New Objectparameter("Customerid", Customerid )); Foreach(MERsCInQuery) Console. Writeline (C. customerid +"---"+ C. companyName );
LINQ to entitiesIt appears as a very thin layer of the object service.Programming LanguageProvides direct query function instead of string-based query. In this case, the objectquery class implements iqueryable and allows it to accept the LINQ Expression Tree, the same method used by OSS to push the esql query to the entityclient provider is used to push the query (as the CCT query expression) in the instance framework ).
By default, XML and binary CLR classes generated from EDM in Visual Studio or using edmgen.exe (the command line tool attached to the Entity Framework) are both serializable in XML and binary, in addition, it is the data convention that sets the navigation attribute to datamembers by default. Therefore, you can create an asmx web service and use an entity instance in the view status or WCF Service.
Similar to most Orm, the Entity Framework currently does not support operations such as creation, update, or deletion using the data operation language (DML. The changes must be applied to objects in the memory, and the entire chart to be maintained may need to be interacted with the database multiple times.
Sample Code:
NorthwindentitiesNorthwindcontext =New Northwindentities();
StringCustomerid = txtcustomerid. Text. Trim (); VaRQuery =FromCInNorthwindcontext. MERs WhereC. customerid = customerid SelectC; Foreach(MERsCInQuery) Console. Writeline (C. customerid +"---"+ C. companyName );