EntityFramework Advantages and Disadvantages

Source: Internet
Author: User
Tags bulk insert

EntityFramework Advantages and Disadvantages

The Entity Framework is recommended by Microsoft.  NET Platform ORM Development component, now put the source code.  Let's discuss the pros and cons and some of the questions below, referred to as EF. The Entity Framework 6 Roadmap is interested in searching the official website.

High-level view: What are the advantages of changing the use of entityframework in an existing system?


The all-in-1-class mapping table of the framework requires the mapping code to be written and is difficult to maintain.
• Maintainable, easy-to-understand code without creating a large data access layer.
• Provide a LINQ query database, which requires less understanding of SQL from the novice developer.
EF can be used as an infrastructure for data services and OData service.

What is the case that does not suggest using EF?


• Real-time applications.
• The database can only be accessed through stored procedures. The advantage of EF is that it is not only on stored procedures when tracking entity state change. (even though EF does have limited support for stored procedures).
• Frequent insert operations (insert), and EF does not support big data bulk inserts.
• Frequent update operations, the goal of the update is primarily when MultiRow (with a single value)
For example: UPDATE table name SET Columa = ten Where columnb =?
This update operation is better used by ExecuteNonQuery (also available from context contexts or directly from ADO).
• Inverse-paradigm table design and high-performance queries. EF generates queries that are difficult to maintain and do not support mapping to non-canonical tables well.

• There are very performance requirements for the program, and each query needs to be monitored.

we are going to load all the data into memory, we recommend using the Entity Framework? What kind of questions should we expect?


• All entities loaded will require many queries and a lot of time.
• Memory overhead.
• There will be delays because EF needs to track the changes of entities and the processing of large collection objects.
EF context contexts are not thread-safe, you should not use a context context across the service.
• If you intend to do this, use EF to load entities, but do not manage them. (Detach the entities from the context).
• Do not use contextual context objects as a cache object (for distributed scenarios). It is not thread-safe, there are some expenses and does not follow the design of decentralized attention (separation of Concern). A better approach is to design caching APIs, such as the AppFabric cache.

Technical Level:Memory leaks:

We have such a scenario that every 10 seconds we open the contextual context to get a single table and close it. The using (entities context = new entities (_connection)) {... ”

After a few hours, this caused a memory leak.
• I suggest you?? Check for memory leaks to make sure that it is the source from EF. EF is an open source, so you can now report any memory leaks and fix it.
• Use the profiler's Vmmap to track the source of the leak.

In this case, should we keep the context open?


• This is not really important because the query will still be executed. However, it is best to close the context, so the cached entities will be garbage collected, otherwise the context and cached entities will be upgraded in the GC to the second generation and will be trapped there for a while.
• Do not create one context context object for each request for WCF.
• The recommended practice is to create a context object for each operation, because the entity item is obsolete once it is added to the database and may result in data corruption or duplicate item

Does the EDMX size have an impact?

EDMX affects the size because all the EDMX data is loaded into memory to create the context contexts.
edmx are loaded into memory, parsing and mapping are created and cached in the AppDomain. You can close and reopen the context, which does not affect the cached edmx mappings.

very many tables.What if all the tables were placed in a single edmx or between several edmx files?

How can I manage a large number of entities in the EDMX? For example, how can you find them?

Modify a specific table in the designer?

• If you split the edmx file between, you can not connect the navigation performance of the entity.
You should build your own model based on your physical model design-if you have a set of independent entities, they can be in different edmx models.
An edmx file handles hundreds of entity types. If the number of entity types is more than 1000, I will take into account several EDMXS (separate and re-examine their own entity model designs, because a system of thousands of entity types seems too much).
• In VS 2012, with enhanced decomposition features and coloring of the designer, see the links below.
The EF Designer has some improvements, such as shading entities that distinguish entity groups.
There are also designers of third-party entities that may get a better user experience.
Keep in mind-EF is an open source and you can give them the requirements and even contribute their own additions, so it's interesting to explore in the EDMX designer.
• You should split the edmx file into different edmx files by each domain/service.

Relationships between tables

lazy and no lazy mode?
• In distributed situations The lazy code is the devil.
• If they need data from a set of tables and then use include, you can use the Lazy load mode

The "Include" command loads the entire table or Objectset original record?
include loading is a column of related entities. EF does not use * queries.

How do I use EF to perform BULK INSERT/update operations?
• Do not do this, you need to use the original SQL statement to implement.

when an entity is marked as modified, EF updates all columns?
• It does not update all columns unless you are using a distributed application with self-tracking. The "normal" implementation under EF only updates the modified columns. Self-tracking does not track the updated properties, it marks that all properties have been updated. You can edit the self-tracking and fix it if needed.
• You can attach an SP for custom logic.
• You can change the file for the T4 template under Self-tracking to keep all the original values.

does the raw update cost much?
• Most db recalculates indexes. This is a problem related to DBAs.

when we update an XML column using EF's XML file for 700-800KB, we receive an exception for tempdb on SQL Server. What's causing it?
• It may involve how the database uses temporal tables to update the contents of XML.

You should check your database with the DBA and you may need to increase the size of your tempdb database.
• In general, large objects, such as XML, make the large object heap memory pressure in GC, and you should consider migrating to the. NET 4.5 version of the GC large object heaps defragmentation feature.
• You can consider storing XML as Byte [], which can be compressed (XML is not a very economical format).

What is the cost of using EF and ADO?
DataReader The cost of transforming the SQL query from LINQ to the cost of the object.

You will have the same overhead and even more if you try to build your own ORM framework. Don't do it!
• The overhead of the abstraction layer, which depends on what they do with EF.
• In general, you should combine alternative inheritance to avoid it having a greater overhead.

EntityFramework Advantages and Disadvantages

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.