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

查看:19
本文介绍了Entity Framework 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 秒.请注意,我在不同的环境中对其进行了测试,例如使用不同的互联网连接.页面没有图片,测试页面只有5行2列数据(调用简单方法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 的常见问题,所以我尝试修复它,同时从帖子中获取帮助 快速实体框架的 3 个步骤预生成模型和视图缓存

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 文件并在手册中选择 Entity Framework > Generate View 来生成视图

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 构建和部署为 Release 包

Build and deployed as Release package with debug=false

出于测试目的,我还上传了没有实体框架的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/

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

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