gpt4 book ai didi

c# - DbContext 加载缓慢

转载 作者:太空宇宙 更新时间:2023-11-03 10:35:26 25 4
gpt4 key购买 nike

我有 WPF 应用程序,在每个 usercontrol.xaml.cs 文件中我都有一个字段

private readonly DBContextManager dbManager = new DBContextManager();

DBContextManager 类:

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

因此,当我第一次打开使用 DbContext 的用户控件状态时,需要 2-4 秒才能加载该用户控件界面。加载后,我可以回到我以前的用户控件状态并再次打开那个用户控件状态,然后延迟就消失了。所以我猜问题是,第一次从DbContext加载数据时,延迟总是存在的。那么是否有解决方案可以避免这种第一次缓慢加载?我首先想到的是让这个 DBContextManager 类成为静态的,或者我会在 MainWindow 中创建一个 DBContextManager 的实例,然后在所有地方使用该实例,但我不确定这是否是个好主意。

我使用的是所有最新版本的 sqlite 和 EF6。

最佳答案

如果您使用的是 Entity-framework 6,一种方法是使用 ngen 工具编译 ef dll,并避免在您的应用程序从 nuget 包加载已安装的 ef 时延迟这样做。您可以在此处查看 ngen 文档。 http://msdn.microsoft.com/en-us/library/6t9t5wcf%28v=vs.110%29.aspx .此外,Julie lerman 在 Pluralsight.com 上有一门关于 ef 6 以及如何加速 Entity Framework 初始化的好类(class) http://www.pluralsight.com/courses/entity-framework-6-ninja-edition-whats-new .

希望对你有帮助

关于c# - DbContext 加载缓慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27998117/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com