码迷,mamicode.com
首页 > Web开发 > 详细

.NET ORM 哪家强

时间:2016-07-15 06:19:37      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:

 

ORM到底哪家强? 很多人都想知道这个问题,自已也没测试过,只能道听途说。

闲的无聊就将几个ORM拿出来比一比,假如怀疑测试代码有问题可以将它下载下来慢慢研究。

 

参赛ORM

 

1、SqlSugar:是一款轻量级的MSSQL ORM ,除了具有媲美ADO的性能外还具有和EF相似简单易用的语法,地址:http://www.cnblogs.com/sunkaixuan/p/5654695.html

 

2、Chloe ORM: 模仿EF代码风格与设计的一款轻量级高性能ORM ,地址:http://www.cnblogs.com/so9527/

 

3、Dapper ORM : 是一个轻型的ORM类。代码就一个SqlMapper.cs文件,主要是IDbConnection的扩展方法,官方站点http://code.google.com/p/dapper-dot-net/ ,也可以通过Nuget进行安装

 

4、EF6.0: .NET官方ORM

 

 

测试结果:

技术分享

 

从测试代码可以看出 SqlSugar Dapper Chloe的性能都是差不多的,EF就有些不尽人意

 

 

将EF改成AsNoTracking又测了几次,【查询所有】性能有所提升但是还是最慢, 查询单条和分页AsNoTracking貌似没什么效果

 

 

测试代码切片

技术分享
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SqlSugar;
using SyntacticSugar;
using Dapper;
using System.Data.SqlClient;
using System;
using Chloe.SqlServer;

namespace ORMTest
{
    /// <summary>
    /// 查询所有
    /// </summary>
    public class SelectAll
    {
        public static void Execute(int count,string title) {

            Console.WriteLine(title);
            PerHelper.Execute(count, " SqlSugar Queryable ", () =>
            {
                using (SqlSugarClient db = new SqlSugarClient(ConfigSugar.GetConfigString("SchoolContext")))
                {

                    var list = db.Queryable<Student>().ToList();

                }
            });

            PerHelper.Execute(count, " SqlSugar Sqlable ", () =>
            {
                using (SqlSugarClient db = new SqlSugarClient(ConfigSugar.GetConfigString("SchoolContext")))
                {

                    var list = db.Sqlable().From<Student>("t").SelectToList<Student>("*");

                }
            });


            PerHelper.Execute(count, " SqlSugar SqlQuery ", () =>
            {
                using (SqlSugarClient db = new SqlSugarClient(ConfigSugar.GetConfigString("SchoolContext")))
                {

                    var list = db.SqlQuery<Student>("select * from Student");

                }
            });


            PerHelper.Execute(count, " Chloe ORM Query", () =>
            {
                using (MsSqlContext context = new MsSqlContext(ConfigSugar.GetConfigString("SchoolContext")))
                {
                    var list = context.Query<Student>().ToList();
                }
            });

            PerHelper.Execute(count, " Chloe ORM  SqlQuery", () =>
            {
                using (MsSqlContext context = new MsSqlContext(ConfigSugar.GetConfigString("SchoolContext")))
                {
                    var list = context.SqlQuery<Student>("select * from Student").ToList();
                }
            });

            PerHelper.Execute(count, " EF6.0 ", () =>
            {
                using (SchoolContext sc = new SchoolContext())
                {
                    var list = sc.Students.AsNoTracking().ToList();
                }
            });


            PerHelper.Execute(count, " Dapper Sql ", () =>
            {
                using (SqlConnection conn = new SqlConnection(ConfigSugar.GetConfigString("SchoolContext")))
                {
                    var list = conn.Query<Student>("select * from Student").ToList(); ;
                }
            });


            Console.WriteLine("");
        }
    }
}
查询所有
技术分享
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SqlSugar;
using SyntacticSugar;
using Dapper;
using System.Data.SqlClient;
using System;
using Chloe.SqlServer;
using Chloe;

namespace ORMTest
{
    /// <summary>
    /// 查询分页
    /// </summary>
    public class SelectPage
    {
        public static void Execute(int count,string title) {

            Console.WriteLine(title);
            PerHelper.Execute(count, " SqlSugar Queryable ", () =>
            {
                using (SqlSugarClient db = new SqlSugarClient(ConfigSugar.GetConfigString("SchoolContext")))
                {

                    var list = db.Queryable<Student>().OrderBy("id").ToPageList(10,20);

                }
            });

            PerHelper.Execute(count, " SqlSugar Sqlable ", () =>
            {
                using (SqlSugarClient db = new SqlSugarClient(ConfigSugar.GetConfigString("SchoolContext")))
                {

                    var list = db.Sqlable().From<Student>("t").SelectToPageList<Student>("*","id",10,20);

                }
            });


            PerHelper.Execute(count, " SqlSugar SqlQuery ", () =>
            {
                using (SqlSugarClient db = new SqlSugarClient(ConfigSugar.GetConfigString("SchoolContext")))
                {

                    var list = db.SqlQuery<Student>("select * from(select *,row_number() over(order by id) as r from Student  ) t where t.r between @b and @e",new { b=181,e=200});

                }
            });

            PerHelper.Execute(count, " Chloe ORM Queryable ", () =>
            {
                using (MsSqlContext context = new MsSqlContext(ConfigSugar.GetConfigString("SchoolContext")))
                {
                    var list = context.Query<Student>().Skip(180).Take(20).ToList();
                }
            });

            PerHelper.Execute(count, " Chloe ORM  SqlQuery", () =>
            {
                using (MsSqlContext context = new MsSqlContext(ConfigSugar.GetConfigString("SchoolContext")))
                {
                    var list = context.SqlQuery<Student>("select * from(select *,row_number() over(order by id) as r from Student  ) t where t.r between @b and @e", new DbParam[] {
                        DbParam.Create("@b", "181"),
                        DbParam.Create("@e", "200")
                    }).ToList();
                }
            });

            PerHelper.Execute(count, " EF6.0 ", () =>
            {
                using (SchoolContext sc = new SchoolContext())
                {
                    var list = sc.Students.OrderBy(it=>it.id).Skip(180).Take(20).AsNoTracking().ToList();
                }
            });


            PerHelper.Execute(count, " Dapper Sql ", () =>
            {
                using (SqlConnection conn = new SqlConnection(ConfigSugar.GetConfigString("SchoolContext")))
                {
                    var list = conn.Query<Student>("select * from(select *,row_number() over(order by id) as r from Student  ) t where t.r between @b and @e", new { b = 181, e = 200 }).ToList(); ;
                }
            });


            Console.WriteLine("");
        }
    }
}
查询分页
技术分享
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SqlSugar;
using SyntacticSugar;
using Dapper;
using System.Data.SqlClient;
using System;
using Chloe.SqlServer;
using Chloe;

namespace ORMTest
{
    /// <summary>
    /// 查询单条
    /// </summary>
    public class SelectSingle
    {
        public static void Execute(int count,string title) {

            Console.WriteLine(title);
            PerHelper.Execute(count, " SqlSugar Queryable ", () =>
            {
                using (SqlSugarClient db = new SqlSugarClient(ConfigSugar.GetConfigString("SchoolContext")))
                {

                    var list = db.Queryable<Student>().Where(it=>it.id==1).ToList();

                }
            });

            PerHelper.Execute(count, " SqlSugar Sqlable ", () =>
            {
                using (SqlSugarClient db = new SqlSugarClient(ConfigSugar.GetConfigString("SchoolContext")))
                {

                    var list = db.Sqlable().From<Student>("t").Where("id=@id").SelectToList<Student>("*", new { id = 1 });

                }
            });


            PerHelper.Execute(count, " SqlSugar SqlQuery ", () =>
            {
                using (SqlSugarClient db = new SqlSugarClient(ConfigSugar.GetConfigString("SchoolContext")))
                {

                    var list = db.SqlQuery<Student>("select * from Student where id=@id",new { id=1});

                }
            });


            PerHelper.Execute(count, " Chloe ORM  Queryable", () =>
            {
                using (MsSqlContext context = new MsSqlContext(ConfigSugar.GetConfigString("SchoolContext")))
                {
                    var list = context.Query<Student>().Where(it=>it.id==1).ToList();
                }
            });

            PerHelper.Execute(count, " Chloe ORM  SqlQuery", () =>
            {
                using (MsSqlContext context = new MsSqlContext(ConfigSugar.GetConfigString("SchoolContext")))
                {
                    var list = context.SqlQuery<Student>("select * from Student where id=@id", DbParam.Create("@id", "1")).ToList();
                }
            });

            PerHelper.Execute(count, " EF6.0 ", () =>
            {
                using (SchoolContext sc = new SchoolContext())
                {
                    var list = sc.Students.Where(it=>it.id==1).AsNoTracking().ToList();
                }
            });


            PerHelper.Execute(count, " Dapper Sql ", () =>
            {
                using (SqlConnection conn = new SqlConnection(ConfigSugar.GetConfigString("SchoolContext")))
                {
                    var list = conn.Query<Student>("select * from Student where id=@id",new { id=1}).ToList(); ;
                }
            });


            Console.WriteLine("");
        }
    }
}
查询单条
技术分享
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SqlSugar;
using SyntacticSugar;

namespace ORMTest
{
    public class PerHelper
    {
        public static void Execute(int count, string title, Action fun)
        {
            PerformanceTest ptef = new PerformanceTest();
            ptef.SetIsMultithread(false);//开启多线程
            ptef.SetCount(count);//执行count次
            ptef.Execute(
                        i =>
                        {
                            fun();

                        },
                        res =>
                        {
                            Console.WriteLine($"执行{count}次,{title}{res}");
                        });

        }
    }
}
性能测试通用函数

 

 

 代码下载

测试代码地址:https://pan.baidu.com/s/1kVnR97D

 

.NET ORM 哪家强

标签:

原文地址:http://www.cnblogs.com/sunkaixuan/p/5672211.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!