实体框架6.2非常慢的首次启动和EFInteractiveViews [英] Entity Framework 6.2 very slow first startup and EFInteractiveViews

查看:163
本文介绍了实体框架6.2非常慢的首次启动和EFInteractiveViews的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个话题已经在stackoverflow和许多其他博客上进行了广泛讨论,提出这个问题的原因是,我观察到这个话题主要是在3到5年的旧帖子中讨论的,而现在我们使用的是EF 6.2版本,并且我希望这可能是已更新(有更多原因会引起您的疑问.

this topic is already widely discussed on stackoverflow and many other blogs, reason to asking question is that i observe this topic was discussed in mostly 3 to 5 years old posts whereas we have EF 6.2 version now, and i expect this may have updates already (there are more reasons you will find in question.

我的应用程序至少有25个使用MySQL作为数据库的模型(表),模型和关系在OnModelCreating中配置,网站托管在godaddy上,但是我无法很好地访问IIS配置等.

My application has at least 25 Models(Tables) with MySQL as database, models and relations are configured in OnModelCreating, web site is hosted on godaddy and i do not have good access to IIS configurations etc.

页面加载时间

  • 首页加载时间:65至70秒
  • 第二页加载时间:1到3秒

再次延迟10分钟后,将需要70秒来加载页面. 请注意,我在不同的环境(例如使用不同的Internet连接)中对其进行了测试. 页面上没有图片,测试页面只有5行数据,两列(调用简单方法db.Test.ToList();)

After 10 minutes delay again it will takes 70 seconds to load page. Please note that i tested it in different environments, like using different internet connection. There are no pictures on the page, and test page has only 5 rows of data with two columns (calling simple method db.Test.ToList();)

在搜索互联网时,我发现EF普遍存在问题,因此我尝试修复它,同时从帖子

While searching internet i found that is common problem with EF so i tried fixing it, while taking help from posts 3 Steps for Fast EntityFramework and Pregenerate Model and View Cache

此修复程序之后

  • 首页加载时间:64到67秒
  • 第二页加载时间:1到3秒

  • First page load: 64 to 67 seconds
  • 2nd page load: 1 to 3 seconds

// DbConfiguration constructor
public MyDbConfiguration
{
     var path = Path.GetDirectoryName(this.GetType().Assembly.Location);
     SetModelStore(new DefaultDbModelStore(path));
}

// DbContext
private static DbMappingViewCacheFactory viewCacheFactory;

private static DbMappingViewCacheFactory ViewCacheFactory
{
    get
    {
        if (viewCacheFactory == null)
        {
            var path =ConfigurationManager.AppSettings[GlobalContextConfig.EFCacheFolder];
            viewCacheFactory=new FileViewCacheFactory(path+"Budget.Context.MyDbContext.xml");
        }
        return viewCacheFactory;
    }
}


public MyDbContext()
    : base("name=MySqlConnectionString")
{
     // In case i need to update xml for now i delete the old file manually 
    InteractiveViews.SetViewCacheFactory(this, ViewCacheFactory);

    Database.SetInitializer<MyDbContext>(null);

    this.Configuration.ProxyCreationEnabled = false;
    this.Configuration.LazyLoadingEnabled = false;
    this.Configuration.AutoDetectChangesEnabled = false;
    this.Configuration.ValidateOnSaveEnabled = false;
}

它已进行了改进,但还不够,我想知道这些问题是否在EF 6.2.0中已更新,或者其修复方法已更改,或者我做错了/应该检查的任何事情.

It was improved but not enough, I want to know if these issues are updated in EF 6.2.0 or method of fixing it has changed, or any thing im doing wrong/should check.

我还安装了EF 6.1.X,并通过右键单击Contaxt文件并在手册中选择实体框架">生成视图"来生成视图

I also installed EF 6.1.X and generated views by right clicking Contaxt file and choosing Entity Framework > Generate View in the manu

结果:

  • 首页加载:40到50秒
  • 第二页加载时间:0到1.5秒

这真是太神奇了,EF 6.1.X比EF 6.2快得多

That's quite amazing, EF 6.1.X is much faster than EF 6.2

使用debug = false

Build and deployed as Release package with debug=false

出于测试目的,我还上传了不带Entity Framework的asp.net应用程序,第一次加载需要8到13秒,而第二次加载不到1秒

For test purpose i also uploaded asp.net application without Entity Framework, it takes 8 to 13 seconds to load for first time, and second load takes in less than 1 seconds

推荐答案

我想知道这些问题是否已在EF 6.2.0中更新或修复方法已更改,或者我做错了/应该检查的任何事情.

I want to know if these issues are updated in EF 6.2.0 or method of fixing it has changed, or any thing im doing wrong/should check.

答案是肯定的

EF 6.2引入了模型缓存

EF 6.2 has introduced a Model Cache

public class MyDbConfiguration : DbConfiguration
{
    public MyDbConfiguration() : base()
    {
        var path = Path.GetDirectoryName(this.GetType().Assembly.Location);
        SetModelStore(new DefaultDbModelStore(path));
    }
}

[DbConfigurationType(typeof(MyDbConfiguration))]
public class MyContextContext : DbContext 
{
}

您可以在此处了解更多信息: https://codeopinion.com/实体框架代码优先模型缓存/

You can learn more here: https://codeopinion.com/entity-framework-code-first-model-cache/

这篇关于实体框架6.2非常慢的首次启动和EFInteractiveViews的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆