问题
I have WPF applicaton, and in every usercontrol.xaml.cs file I have a field
private readonly DBContextManager dbManager = new DBContextManager();
Class DBContextManager:
public class DBContextManager : DbContext {
public DBContextManager() : base("App_DbContext") {
Database.SetInitializer<DBContextManager>(null);
}
public DbSet<Person> Persons { get; set; }
}
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.
I'm using all latest version of sqlite and EF6.
回答1:
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.
Hope this helps
来源:https://stackoverflow.com/questions/27998117/dbcontext-slow-loading