entity framework与ADO.NET 2.0效率

拿entity framework与原始的ADO.NET 2.0对比看看。

我一直很犹豫该不该在项目使用entity framework,因为用ADO.NET 2.0,搭配codesmith开发效率也是挺高的,并且ADO.NET对团队的素质要求相对没entity framework那么高,在公司资源有限,无法招揽些开发能力较高的程序员的情况下,是否该用entity framework呢?

这篇文章没有要得出什么结论,纯粹是我学习entity framework的过程中的一些试验笔记。

建了个表,对应的实体:

public class User
{
    [Key]
    public int UserId { get; set; }
    public string Name { get; set; }
    public string Address { get; set; }
}

1.运行:

    static void Main(string[] args)
    {
        using(MyContext mc = new MyContext())
        {
            User u = new User { Name = "test3", Address = "some address" };

            Console.WriteLine(DateTime.Now.ToLongTimeString());
            //mc.Configuration.AutoDetectChangesEnabled = false;
            mc.Users.Add(u);
            //mc.Configuration.AutoDetectChangesEnabled = true;
            Console.WriteLine(DateTime.Now.ToLongTimeString());
            mc.SaveChanges();
            Console.WriteLine(DateTime.Now.ToLongTimeString());
        }
    }
image.png

看起来,Add方法比较耗时;

2.把AutoDetectChangesEnabled设置为false运行:

    static void Main(string[] args)
    {
        using(MyContext mc = new MyContext())
        {
            User u = new User { Name = "test3", Address = "some address" };

            Console.WriteLine(DateTime.Now.ToLongTimeString());
            mc.Configuration.AutoDetectChangesEnabled = false;
            mc.Users.Add(u);
            mc.Configuration.AutoDetectChangesEnabled = true;
            Console.WriteLine(DateTime.Now.ToLongTimeString());
            mc.SaveChanges();
            Console.WriteLine(DateTime.Now.ToLongTimeString());
        }
    }
image.png

看起来效果差不多。

3.如果insert 10000行数据:

    static void Main(string[] args)
    {
        using(MyContext mc = new MyContext())
        {
            try
            {
                Console.WriteLine(DateTime.Now.ToLongTimeString());
                //mc.Configuration.AutoDetectChangesEnabled = false;
                for (int i = 0; i < 10000; i++)
                {
                    User u = new User { Name = "test" + i.ToString(), Address = "some address" };
                    mc.Users.Add(u);
                }
            }
            finally
            {
                //mc.Configuration.AutoDetectChangesEnabled = true;
            }
            Console.WriteLine(DateTime.Now.ToLongTimeString());
            mc.SaveChanges();
            Console.WriteLine(DateTime.Now.ToLongTimeString());
        }
    }
image.png

看起来,Add方法特别耗时,注意:如果这里使用mc.Users.AddRange(XXX)去insert10000行数据,速度就会比较快;

4.把AutoDetectChangesEnabled设置为false运行insert 10000行数据:

    static void Main(string[] args)
    {
        using(MyContext mc = new MyContext())
        {
            try
            {
                Console.WriteLine(DateTime.Now.ToLongTimeString());
                mc.Configuration.AutoDetectChangesEnabled = false;
                for (int i = 0; i < 10000; i++)
                {
                    User u = new User { Name = "test" + i.ToString(), Address = "some address" };
                    mc.Users.Add(u);
                }
            }
            finally
            {
                mc.Configuration.AutoDetectChangesEnabled = true;
            }
            Console.WriteLine(DateTime.Now.ToLongTimeString());
            mc.SaveChanges();
            Console.WriteLine(DateTime.Now.ToLongTimeString());
        }
    }
image.png

速度快很多。

5.普通ADO.NET insert 10000行数据:

    static void Main(string[] args)
    {
        using(MyContext mc = new MyContext())
        {
            string sql = "insert into Users(Name,Address) values (@Name,@Address) ";
            Console.WriteLine(DateTime.Now.ToLongTimeString());
            for (int i = 0; i < 10000; i++)
            {

                System.Data.SqlClient.SqlParameter[] paras = {

                 new System.Data.SqlClient.SqlParameter("@Name ",  System.Data.SqlDbType.NVarChar),
                 new System.Data.SqlClient.SqlParameter("@Address ",  System.Data.SqlDbType.NVarChar)
                };
                paras[0].Value = "test" + i.ToString();
                paras[0].Value = "some address";
                SqlHelper.sqlToExecute(sql, paras);
            }
            Console.WriteLine(DateTime.Now.ToLongTimeString());
        }
    }
image.png

速度确实是最快的。


以上是insert的,现在试试update 10000 行数据:

    static void Main(string[] args)
    {
        using(MyContext mc = new MyContext())
        {
            try
            {
                var lst = mc.Users.ToList();//数据库有10000行数据
                Console.WriteLine(DateTime.Now.ToLongTimeString());
                //mc.Configuration.AutoDetectChangesEnabled = false;
                foreach (var a in lst)
                {
                    a.Address = a.Address + "1000";

                }
             
            }
            finally
            {
                //mc.Configuration.AutoDetectChangesEnabled = true;
            }
            Console.WriteLine(DateTime.Now.ToLongTimeString());
            mc.SaveChanges();
            Console.WriteLine(DateTime.Now.ToLongTimeString());
        }
    }
image.png

看起来,update的速度是很让人满意的。


那么delete呢?

    static void Main(string[] args)
    {
        using(MyContext mc = new MyContext())
        {
            try
            {
                var lst = mc.Users.ToList();
                Console.WriteLine(DateTime.Now.ToLongTimeString());
                //mc.Configuration.AutoDetectChangesEnabled = false;
                foreach (var a in lst)
                {
                    mc.Users.Remove(a);
                }
                 //如果直接用mc.Users.RemoveRange(lst);,也会快的
            }
            finally
            {
                //mc.Configuration.AutoDetectChangesEnabled = true;
            }
            Console.WriteLine(DateTime.Now.ToLongTimeString());
            mc.SaveChanges();
            Console.WriteLine(DateTime.Now.ToLongTimeString());
        }
    }
image.png

看起来特别慢;

然而如果加上mc.Configuration.AutoDetectChangesEnabled = false,速度就上来了:

    static void Main(string[] args)
    {
      
        using(MyContext mc = new MyContext())
        {
            try
            {
                var lst = mc.Users.ToList();
                Console.WriteLine(DateTime.Now.ToLongTimeString());
                mc.Configuration.AutoDetectChangesEnabled = false;
                foreach (var a in lst)
                {
                    mc.Users.Remove(a);
                }
             
            }
            finally
            {
                mc.Configuration.AutoDetectChangesEnabled = true;
            }
            Console.WriteLine(DateTime.Now.ToLongTimeString());
            mc.SaveChanges();
            Console.WriteLine(DateTime.Now.ToLongTimeString());
        }
    }
image.png

你可能感兴趣的:(entity framework与ADO.NET 2.0效率)