正如标题所示,我在使用实体框架对SQL Server数据库进行第一次查询时遇到问题.我试过寻找答案,但似乎没有人真正找到解决方案.
测试是在使用Entity Framework 6的Visual Studio 2012中完成的,我还使用了T4视图模板来预编译视图.该数据库位于SQL Server 2008上.我们有大约400个POCO(400个映射文件),在数据库表中只有100行数据.
捕获后是我的测试代码和结果.
static void Main(string[] args){
Stopwatch st=new Stopwatch();
st.Start();
new TestDbContext().Set<Table1>.FirstOrDefault();
st.stop();
Console.WriteLine("First Time "+st.ElapsedMilliseconds+ " milliseconds");
st.Reset();
st.Start();
new TestDbContext().Set<Table1>.FirstOrDefault();
st.stop();
Console.WriteLine("Second Time "+st.ElapsedMilliseconds+ " milliseconds");
}
Run Code Online (Sandbox Code Playgroud)
检测结果
First Time 15480 milliseconds
Second Time 10 milliseconds
Run Code Online (Sandbox Code Playgroud)