的DbContext加载速度慢 [英] DbContext Slow Loading

查看:669
本文介绍了的DbContext加载速度慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有WPF的一个应用,并在每usercontrol.xaml.cs文件我有一个字段

I have WPF applicaton, and in every usercontrol.xaml.cs file I have a field

private readonly DBContextManager dbManager = new DBContextManager();



类DBContextManager:

Class DBContextManager:

public class DBContextManager : DbContext {
    public DBContextManager() : base("App_DbContext") {
        Database.SetInitializer<DBContextManager>(null);
    }
    public DbSet<Person> Persons { get; set; }
}



所以,当我打开用户控件,它使用的DbContext状态第一次,它需要2-4s加载之前用户控件界面会显示出来。一旦加载,我可以回到我以前的用户控件的状态和开拓再次证明usecontrol状态,则延迟了。所以我想这个问题是,延迟将一直存在,当谈到从加载数据的DbContext的第一次。那么,有没有一个解决方案,以避免这种情况首先加载速度慢?首先我的想法,就是让这个DBContextManager类的静态,否则我将在主窗口创建DBContextManager的一个实例,然后在任何地方使用该实例,但我不知道这是好主意。

So, first time when I open usercontrol state which uses DbContext, it takes 2-4s to load before that usercontrol interface will show up. Once it loaded, I can go back to my previous usercontrol state and open up again that usecontrol state, then the delay is gone. So I guess the problem is, delay will always exist when it comes to load data from DbContext for a first time. So is there a solution to avoid this first slow loading? First what I thought, was to make this DBContextManager class static, or I would create an instance of DBContextManager in the MainWindow, and then use that instance everywhere, but I'm not sure if that is good idea.

我用所有最新版本SQLite和EF6的。

I'm using all latest version of sqlite and EF6.

推荐答案

如果您使用的是实体框架6,一种办法是使用NGEN工具编译EF DLL和避免这样做的延迟,当你的应用程序加载安装弗朗的NuGet包EF。你可以看看这里NGEN文档。 HTTP ://msdn.microsoft.com/en-us/library/6t9t5wcf%28v=vs.110%29.aspx 。同时朱莉·勒曼大约有EF 6以及如何在Pluralsight.com一个很好的课程,加速比实体框架这里的 http://www.pluralsight.com/courses/entity-framework-6-ninja-edition-whats-new

If you are using Entity-framework 6, one way is to use ngen tool to compile the ef dll and avoid the delay of doing that when your application loads the ef installed fron nuget package. You can have a look to ngen doc here.http://msdn.microsoft.com/en-us/library/6t9t5wcf%28v=vs.110%29.aspx. Also Julie lerman has a good course on Pluralsight.com about ef 6 and how to speedup initialization of Entity-framework here http://www.pluralsight.com/courses/entity-framework-6-ninja-edition-whats-new.

希望这有助于

这篇关于的DbContext加载速度慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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