EF6,EF5,SQLConnection。首次加载数据。时差很大。如何提高EF6的首次加载速度?

时间:2015-02-20 13:52:03

标签: c# entity-framework ado.net entity-framework-5 sqlconnection

我最近做了一些从SQL Server 2008 RT检索数据的性能测试。我使用Entity Framework 5,Entity Framework 6和ADO.NET(SQLConnection类)。当我调用我的应用程序时,应用程序会加载数据。所以这是应用程序中的第一个数据加载。之后,CRUD操作的EF 6的速度更快,并且没有等待执行。

我很惊讶SQLConnection类和实体框架6的性能提升之间的时差:

  1. 测试发生在本地网络中。速度为100Mbit / s。
  2. 计算SQL Server表“People”中的项目:778人。
  3. SQLConnection的时间:00:00:00.1231548
  4. 实体框架时间6:00:00:05.7594701(五秒!)
  5. 实体框架时间5:00:00:03.1855048(三秒!)
  6. SQLConnection代码:

    SQLConnection的时间:00:00:00.1231548

                Stopwatch timer = new Stopwatch();
                timer.Start();
                string connstr = "Data Source=FooSQLSERVER;initial catalog=FooCatalog;Persist Security Info=True; MultipleActiveResultSets=True;";
                SqlConnection conn = new SqlConnection();
                conn.ConnectionString = connstr;
                SqlCommand fooCommand = conn.CreateCommand();
                fooCommand.CommandText = "SELECT IDUser, Name, LOGIN, TELEPHONE FROM PEOPLE";
                conn.Open();
                SqlDataReader fooReader = fooCommand.ExecuteReader();                
                int id;
                string name, login, phone;
                while (fooReader.Read())
                {
                    id = Convert.ToInt32(fooReader["IDUSER"]);
                    name = Convert.ToString(fooReader["Name"]);
                    login = Convert.ToString(fooReader["LOGIN"]);                    
                    phone = Convert.ToString(fooReader["TELEPHONE"]);
                    collEmpl.Add(new Employee() { ID = id, NAME = name, LOGIN = login, TELEPHONE = phone });
                } conn.Close();
                timer.Stop();
    

    EntityFramework 5 Code和EntityFramework 6代码相同(但结果不同):

    实体框架6的时间:05.7594701(五秒!)

    实体框架时间5:00:00:03.1855048(三秒!)

            Stopwatch timer = new Stopwatch();
            timer.Start();
            using (FooEntities context = new FooEntities())
            { 
                db.People.ToList();
                timer.Stop();
            }
    

    1。我的问题是如何实现与SQLConnection类似的加载Entity Framework 6数据的速度?

    2。 SQLConnection的速度增益是否超过EF6正常?

    第3。为什么Entity Framework 6比实体框架5慢?

0 个答案:

没有答案
相关问题