Entity Framework Part2

Source: Internet
Author: User

EF Principles
Open the edmx file in XML, the XML file contains two main parts: Runtime is the class model part, designer is the graphical interface in VS
The main discussion is the runtime section, which is divided into three parts:
SSDL Data Model Section
CSDL Conceptual Model Section
C-s Mapping Mapping Rules section
Drawing demonstrates the correspondence between ORM and EDMX
Completion of SQL generation based on mapping relationships and entity state

The Entity Framework takes advantage of the abstraction of data structures to convert each database object into an Application object (entity), and the data fields are converted to properties, and the relationships are converted to associative attributes (association), so that the database's e/r The model is completely turned into an object model, so that programmers can invoke access in the most familiar programming language. In the abstract structure, it is a highly integrated and corresponding structure of the concept layer, the corresponding layer and storage layer, as well as supporting the Entity Framework of the data provider (provider), so that the work of data access is smooth and complete.

Concept Layer E: Responsible for upward object and attribute exposure and access
The corresponding layer m: the above concept layer and the underlying storage layer of the data structure of the corresponding together.
Storage Layer D: According to different database and data structure, and reveal the entity data structure, together with Provider, responsible for the actual access to the database and the generation of SQL

Gateway for manipulating databases in EF: Context
ObjectContext encapsulates the connection between the. NET Framework and the database. This class is used as a gateway for Create, read, update, and delete operations.
The ObjectContext class is used as the primary class for interacting with data as objects that are instances of the entity types defined in the EDM.
An instance of the ObjectContext class encapsulates the following:
A connection to the database, encapsulated as a EntityConnection object.
Describes the metadata of the model, encapsulated as a MetadataWorkspace object
Objectstatemanager object for managing persisted objects in the cache
Note: The entry for EF access to the database is ObjectContext in the previous version of EF4.0. In the later version of EF4.1, the entry to the latest database access context was changed to DbContext. Essentially: DbContext inherits the ObjectContext class, so the new version cannot directly use the methods and properties inside the ObjectContext, but the underlying things are still the same.

EF's advantages over ADO
Greatly improve the development efficiency, EF is Microsoft's own products, and VS development tools integration is better, the development of code are strong type, writing code is very efficient, very high automation program, imperative programming
The model designer provided by EF is very powerful, not only for the revolution in design databases, but also for the ability to automate the generation of model code that greatly improves the efficiency of development and architecture design.
EF Cross-database support is one of the main functional points of the ORM framework, and can be achieved by simply altering the configuration to achieve cross-database capabilities
BUG: Poor performance (generation of SQL Script phase), SQL scripts generated when complex queries are not very efficient

The disadvantage of EF relative to ADO
Q 1:ef performance is good or not?
Answer 1: Not good, performance is lossy
Q 2: Where is the loss?
Answer 3: The database side performance loss is the same; loss of performance when converting an object state to an SQL statement, but it is very convenient to replace the database

Model First
At the beginning of the project, when there is no database, you can design the model with EF and then synchronize the model to complete the creation of the table in the database, that is the Modelfirst development method
Example: Creating a model BookInfo, BookType

Type of property:
The types here are the types in the CTS, that is, the types used in IL
Int32
String, you can choose whether to use Unicode encoding, and if so, corresponding to the nvarchar type in SQL Server
A Decimal that represents the type of the specified scale and data precision, the range represents the number of decimal digits, and the precision displays the total number of data bits
Property "can be null"
Property "Entity key": Indicates setting primary key

Association:
1:1 performance is low (do not delay loading, add must create two objects at the same time), do not use, you can implement the logic code to do this, you can look at the table structure, essentially or 1:m structure
1:m
M:N: You can manually create an intermediate table with 1:m relationships, or you can use this relationship directly, and EF automatically creates an intermediate table
When you create an association, you can choose whether you want to create a navigation property, a foreign key
Navigation properties
View the types of generated navigation properties, depending on the relationship
Example: 1-to-many relationships, inserts for multi-port table data

Method SaveChange (): Executes all the command trees, using the mechanism of things to execute
Synchronous
Updating a model from a database
Generating a database from a model: deleting a table and then creating a table can result in the loss of the original data in the table; Suggest manually modifying the table structure

codefirst
For projects that already have model types, how do you use EF? Code first, also known as Poco+code only
code only, as the name implies, requires no EDMX model. EF provides the ability to generate SQL from the type's structure inference and to create tables in the database, and to infer the relationship between entities through the members of the type, and developers need only write entity classes to make EF-to-database development
advantages: further simplification of development. The efficiency of development is once again improved. The degree of automation is further improved. Can be applied to legacy projects
disadvantage: performance is not good. Fewer people to know

Introducing Assembly Entityframework,system.data.entity
Write the connection string in the configuration file
Create a model class (if you already have a model class in your project, you only need to maintain relationships)
To represent the relationship of a class by navigation properties, note: The Navigation property is set to virtual, which allows for lazy loading
Feature Maintenance: Table,key,foreignkey
Create context class, inherit from DbContext
Call the parent class construction method, passing the connection string "name=***"
To create a database table from a type
Use the context. Database.createifnotexists () Completing the creation of tables in the database
Call the context. SaveChanges () method to complete the Save
Key: Context, constraints and relationships of entity classes

Using the three layers of EF and MVC

Entity Framework Part2

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.