Entity Framework (solid frame EF)

Source: Internet
Author: User

What is the Entity Framework (hereinafter referred to as EF)?

The EF (Entity Framework) is a set of technologies in ADO that support the development of data-oriented software applications and is an ORM framework for Microsoft. ORM (Object Relational Mapping Framework): Refers to the transformation of the object-oriented object model and the data structure of the relational database.

That's an official explanation, you know? I understand that business entities behave as objects in memory, in the database as data, in memory between objects, with associations and inheritance relationships, and in the database, relational data cannot directly express these relationships. and object-relational mapping (ORM) is the solution to this problem. ORM, as a middleware, implements the data mapping of program objects to relational databases. So, EF is a tool that implements the mapping of entities in databases and programs. (My understanding, if wrong, welcome to point out)

What is the core of EF?

At the heart of EF is the EDM (Entity Data Model), which is a specification that defines the data used by applications built on the Entity Framework. applications that use the EDM define entities and relationships in the application domain in the design schema. The design schema is used to build a programmable class that is used by application code. The storage structure that persists application data in this model is represented by another schema, called the storage schema. The mapping specification is used to connect the design schema to the storage schema. The simple understanding is to define the correspondence between the entity and the database.

EDF has three concepts: a conceptual model (conceptual schema definition language file [. csdl]), a mapping (mapping specification language file [. MSL]), and a storage model (storage schema definition language file [. ssdl]). The combination of these three is EDM mode. EDM mode is represented in a project as a file with an. edmx extension. This file that contains the EDM can be designed using the EDM designer in VS, because the file is an XML file that you can manually edit to customize the three parts of CSDL, MSL, and SSDL.

edm--csdl

The CSDL defines the EDM or the soul part of the entire program-the conceptual model. Conceptual models can be understood as entity classes. Entity classes are one of the most fundamental components of object-oriented design, which advance the display of objects in the world as an object design method that can be represented in a calculation. The CSDL of the EDM is to achieve such a purpose. Simply put, it is through the entity, to achieve a data dump or storage or update, so as to calculate.

Code structure: Schema is the root element of the CSDL, where the defined namespace is the namespace used for ObjectContext and Entityclass, and the alias-alias specifies an easy-to-remember name for this namespace namespace After defining alias, element in this schema can be the alias for namespace. (code example below)

 <!--SSDL Content-<edmx:StorageModels> <schema namespace= "Newssystemmodel.store" provider= "System.Data.SqlCl Ient "providermanifesttoken=" "alias=" Self "xmlns:store=" http://schemas.microsoft.com/ado/2007/12/edm/ Entitystoreschemagenerator "xmlns:customannotation=" http://schemas.microsoft.com/ado/2013/11/edm/ Customannotation "xmlns=" http://schemas.microsoft.com/ado/2009/11/edm/ssdl "> <entitytype name=" category "  > <Key> <propertyref name= "id"/> </Key> <property name= "id" type= "int" storegeneratedpattern= "Identity" nullable= "false"/> <property name= "Name" type= "varchar" maxle Ngth= "" nullable= "false"/> </entitytype> 
<span style= "White-space:pre" ></span><!--omit n entitytype and association--> <association name= "FK _news_category "> <end role=" category "Type=" Self.category "multiplicity=" 0..1 "/> <end role= "News" type= "self.news" multiplicity= "*"/> <ReferentialConstraint> <principal role= "Categ Ory "> <propertyref name=" id "/> </Principal> <dependent role=" News "&G              T <propertyref name= "CaId"/> </Dependent> </ReferentialConstraint> </associ ation> <entitycontainer name= "Newssystemmodelstorecontainer" > <entityset Name= "category" Entit Ytype= "self.category" schema= "dbo" store:type= "Tables"/> <entityset name= "comment" entitytype= "self.commen T "schema=" dbo "store:type=" Tables "/> <entityset name=" News "entitytype=" self.news "schema=" dbo "Store:typ E= "Tables" /> <entityset name= "sysdiagrams" entitytype= "self.sysdiagrams" schema= "dbo" store:type= "Tables"/> <associationset name= "Fk_comment_news" association= "Self.fk_comment_news" > <end Role= "News" Entit yset= "News"/> <end role= "comment" entityset= "comment"/> </AssociationSet> &l T AssociationSet name= "Fk_news_category" association= "self.fk_news_category" > <end Role= "category" EntitySe t= "category"/> <end role= "News" entityset= "News"/> </AssociationSet> </enti Tycontainer> </Schema> </edmx:StorageModels>

edm--ssdl

This file describes the concepts that exist in databases such as tables, columns, relationships, primary keys, and indexes. The code structure of SSDL is similar to CSDL.

Edm--msl

This file corresponds to the CSDL and SSDL described above, mainly including the attributes in the CSDL and the columns in SSDL. The MSL root node is mapping, which can contain multiple entitycontainermapping (only one), Each of the entitycontainermapping corresponds to two entitycontainer from CSDL and SSDL respectively. This entitycontainermapping is a description of the correspondence between the two EntityContainer. Here is a section of code to show the basic format of entitycontainermapping.

EF Instances

Examples are as follows, operation database implementation of additions and deletions to search

 static void Main (string[] args) {//All operations on a database the first is to create a context for data access newssystementities DbContext =            New Newssystementities ();            #region Increase///Modify Category Category = new category ();            Category.id = 9;            Category.name = "age and Half"; Dbcontext.entry<category> (category).            state = System.Data.EntityState.Modified; Dbcontext.entry<category> (category).            state = entitystate.added;             Dbcontext.savechanges ();            #endregion #region Modify//Modify category Category = new category ();            Category.id = 30;            Category.name = "New News"; Dbcontext.entry<category> (category).            state = System.Data.EntityState.Modified; Dbcontext.entry<category> (category).            state = entitystate.added;           Dbcontext.savechanges (); #endregion #region Delete//modify category Category = new category ();           Category.id = 30;           Category.name = "New News"; Dbcontext.entry<category> (category).            state = System.Data.EntityState.Modified; Dbcontext.entry<category> (category).            state = entitystate.deleted;            Dbcontext.savechanges (); #endregion #region Query and modify//Query the entity: The default is the tracking state var item = DbContext.category.FirstOrDefault ()            ;             Item.name = "Lirui";//As long as the property is changed, then the state of this entity is automatically changed to modified Dbcontext.savechanges (); #endregion}

advantages and disadvantages of EF

Pros: The definition of an entity class can have a separate project using C#class to accomplish such a design transformation to use an XML file definition and integration into the data access layer. To increase an entity and map it to a database by dynamically changing the EDM's method. Easy to modify database and so on.

Insufficient: The efficiency of Entity framework technology is almost the only slight disadvantage. First of all, the conversion of Entitysql to SQL is interpreted as an interpretative conversion with poor performance. In addition, the Entity Framework needs to read the EDM at each application startup, which is slower (but no longer exists in subsequent operations).


Entity Framework (solid frame EF)

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.