Pits and solutions used in Dbcompiledmodel in the Entity framework

Source: Internet
Author: User

In the previous period, the entity Framework Code first method was introduced when the company was doing the project.

Our company's software for the SaaS structure, there are n Enterprise registration, where SQL Server has a table for the T_user_ Enterprise registration number, such as the 1000th to register the enterprise, for it to generate t_user_1000 this data sheet.

In this way, how to generate a user object corresponding to the data table relationship? Directly in the onmodelcreating directly totable to bind?

If you know a little bit about the implementation details of the Entity Framework, there is a big problem with this approach, which is that onmodelcreating is only called once.

If you are in the code two Times new out of this Model1, as follows:

In this case, the second time new Model1 (10001), the corresponding table in the model is t_user_1000. Because after the second new Model1, the onmodelcreating! is not called during operation. The onmodelcreating is only called once, and the corresponding cache is made.

So how do we make the second creation of the model also invoke onmodelcreating in this way? The way is: there is no way! We can only work in the model's constructor, and we need to put the Model1 constructor that I have created into the model, and the code is as follows:

For convenience, I write a static method to generate the class

This is not the perfect solution to the enterprise number and enterprise-related data sheet of the corresponding relationship? Seemingly solved, but actually hides a big hole.

If we use the following code to call.

What's the result? Memory explodes until out of memory! Each time Createmodel, the Entity Framework is related to the corresponding and cached data, even if the same correspondence (such as 1000 and 10001), in the Entity Framwork framework will be cached 10,000 times respectively.

In this case, what are the workarounds? The only workaround is to cache the Dbcompiledmodel itself. As shown in the following code:

With this scheme, when we invoke the 10,000-time creation process, the Entity framework only creates a cache for enterprise 1000th and number 10,001th respectively.

But this program also has a hidden problem, currently we have hundreds of registered enterprises, so in Dict cache hundreds of enterprise Dbcompiledmodel is no problem, but when our number of registered enterprises reached tens of thousands of years, As the dbcompiledmodel of tens of thousands of enterprises are to be cached in Dict, the memory will be blown away. We are currently analyzing the source of the Entity Framework to find a better solution.

Pits and solutions used in Dbcompiledmodel in the Entity framework

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.