The cognition of MVC's Learning-ef

Source: Internet
Author: User

1. What is EF

EF is also known as the Persistence layer framework: the variables defined in C # are saved to memory, and a power outage is lost. The persistent meaning is that the data is saved to the hard disk (the SQL query of the database is carried out on the hard disk, so it is very slow). EF helps us save an object to the database, and the framework automatically generates the appropriate SQL for us, sending commands to the database via ADO.

What does 2.EF contain?

A. How to create an EF file:

In VS, create a new ADO Entity Data Model. It will generate a file with a suffix of edmx for us. EF did three things for us: Generate an XML file (T4 template through the XML new production entity class and context object), create an entity class (containing the information to process the object), add a data context (EF context through which you can add in-memory data to the database)

By looking at it, the EDMX is actually an XML file. It mainly consists of three parts: Data entity information, model object information, mapping information. There is also an EDMX designer (designer)

Data Entity (Storagemodels)

It contains information about the database (what database, database version, database provider), table information (table name, table's primary key, information about the fields in the table, entity container information)

<Edmx:storagemodels>      <SchemaNamespace= "Mydbmodel.store"Provider= "System.Data.SqlClient"ProviderManifestToken= "+"Alias= "Self"Xmlns:store= "Http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator"xmlns= "HTTP://SCHEMAS.MICROSOFT.COM/ADO/2009/11/EDM/SSDL">        <EntityTypeName= "Users">          <Key>            <PropertyRefName= "Id" />          </Key>          < PropertyName= "Id"Type= "int"StoreGeneratedPattern= "Identity"Nullable= "false" />          < PropertyName= "Name"Type= "nvarchar"MaxLength= " the" />          < PropertyName= "Age"Type= "int" />        </EntityType>        <EntityContainerName= "Mydbmodelstorecontainer">          <EntitySetName= "Users"EntityType= "Self.users"Schema= "dbo"Store:type= "Tables" />        </EntityContainer>      </Schema>    </Edmx:storagemodels>

Model Object (Conceptualmodels)

It contains information about the object (namespace), the primary key (which must be set), the information about the properties of the object, the object container

<Edmx:conceptualmodels>      <SchemaNamespace= "Mydbmodel"Alias= "Self"Annotation:usestrongspatialtypes= "false"xmlns:annotation= "Http://schemas.microsoft.com/ado/2009/02/edm/annotation"xmlns= "HTTP://SCHEMAS.MICROSOFT.COM/ADO/2009/11/EDM">        <EntityTypeName= "User">          <Key>            <PropertyRefName= "Id" />          </Key>          < PropertyName= "Id"Type= "Int32"Nullable= "false"Annotation:storegeneratedpattern= "Identity" />          < PropertyName= "Name"Type= "String"MaxLength= " the"fixedlength= "false"Unicode= "true" />          < PropertyName= "Age"Type= "Int32" />        </EntityType>        <EntityContainerName= "Mydbentities"annotation:lazyloadingenabled= "true">          <EntitySetName= "Users"EntityType= "Self.user" />        </EntityContainer>      </Schema>    </Edmx:conceptualmodels>

Mapping Information (Mappings)

Map that contains table information, and the mappings for each field in the table

<edmx:mappings>      <MappingSpace= "C-s"xmlns= "Http://schemas.microsoft.com/ado/2009/11/mapping/cs">        <entitycontainermappingStorageEntityContainer= "Mydbmodelstorecontainer"CdmEntityContainer= "Mydbentities">          <entitysetmappingName= "Users">            <EntityTypeMappingTypeName= "Mydbmodel.user">              <MappingFragmentStoreEntitySet= "Users">                <ScalarPropertyName= "Id"ColumnName= "Id" />                <ScalarPropertyName= "Name"ColumnName= "Name" />                <ScalarPropertyName= "Age"ColumnName= "Age" />              </MappingFragment>            </EntityTypeMapping>          </entitysetmapping>        </entitycontainermapping>      </Mapping>    </edmx:mappings>  </Edmx:runtime>

B.ef copied the assembly for us

Replication here is because the imported assembly itself is not in the native framework, it resembles a third-party development package, and when you add an entity program, it is copied to the package folder in your project

#region Assembly EntityFramework.dll, v6.0.0.0
E:\ConsoleApplication1\packages\EntityFramework.6.0.0\lib\net45\EntityFramework.dll
#endregion

Lazy Loading in 3.EF

First, the two where () method

A. where () of the collection:

Information can be obtained from:

1. The WHERE () method in the collection is an extension method

2. The extension method is an extension to the interface IEnumerable, and the method returns a bool type

3. This method is under the System.Linq namespace

B. The WHERE () method in the context

From which you can get information

1. where () in context there is an extension to IQueryable
2. The namespace is also under System.Linq

Add the methods in the Where () above, set breakpoints, and monitor list1 data types, such as

Know the type of return is Dbquery

By looking at the definition, it is known that lazy loading is based on System.Linq.Queryable adding extension methods to IQueryable, while deferred loading is really implemented through Dbquery.

C. Why lazy loading is used

Reason one: The query condition "context" may currently be combined by multiple Sqo (standard query) methods. User.order (). Where (). Select (), and each simply adds a query condition to determine whether the query condition has ended. Therefore, there is no way in each Sqo method to determine what the SQL statement is, can only return a Dbquery object containing all the added day sword, when using this Dbquery object, only according to the desired conditions to generate the corresponding SQL statement, query data

Reason two: The delay for foreign key entities, for foreign key properties. Temporary Query foreign key table is also called delayed loading used to check, EF will use this foreign key properties when the corresponding table, rather than a breath of foreign key all out.

The cognition of MVC's Learning-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.