本文共 5003 字,大约阅读时间需要 16 分钟。
目录
Light.Data 是一个基于dotnet standard 2.0的轻量级ORM框架 ,通过实体模型类 Attribute 或配置文件关联数据表。使用核心类 DataContext 在表上进行 CURD 操作。
PM> Install-Package Light.Data
数据库 | 介绍 |
SqlServer的 | 需要安装 nuget 安装 Light.Data.Mssql 库,支持SqlServer 2008或以上版本 |
Mysql的 | 需要安装 nuget 安装 Light.Data.Mysql 库,支持Mysql5.5或以上版本 |
Postgre | 需要安装 nuget 安装 Light.Data.Postgre 库,支持PostgreSQL9.3或以上版本 |
{ "lightData": { "connections": [ { "name": "mssql_db", "connectionString": "...", "providerName": "Light.Data.Mssql.MssqlProvider, Light.Data.Mssql" }, { "name": "mysql_db", "connectionString": "...", "providerName": "Light.Data.Mysql.MysqlProvider, Light.Data.Mysql" } ] }}
// direct useDataContext context = new DataContext("mssql");// create subclasspublic class MyDataContext : DataContext{ public MyDataContext() : base("mssql") { }}// create subclass with optionspublic class MyDataContext : DataContext{ public MyDataContext(DataContextOptionsoptions) : base(options) { }}// direct config connect string and params (IServiceCollection)service.AddDataContext (builder => { builder.UseMssql(connectionString); builder.SetTimeout(2000); builder.SetVersion("11.0");}, ServiceLifetime.Transient);// use default configuration file to config (IServiceCollection)service.AddDataContext (DataContextConfiguration.Global, config => { config.ConfigName = "mssql";}, ServiceLifetime.Transient);
[DataTable("Te_User", IsEntityTable = true)]public class TeUser{ ////// Id /// ///[DataField("Id", IsIdentity = true, IsPrimaryKey = true)] public int Id { get; set; } /// /// Account /// ///[DataField("Account")] public string Account { get; set; } /// /// Telephone /// ///[DataField("Telephone", IsNullable = true)] public string Telephone { get; set; } ....}
在派生类TeUserAndExtend中添加类型为TeUserExtend的公共属性Extend,并添加特性RelationFieldAttribute。在查询TeUserAndExtend中,还将检测关联的TeUserExtend 数据。并且还支持一对多。
[DataTable("Te_UserExtend", IsEntityTable = true)]public class TeUserExtend{ [DataField("Id", IsIdentity = true, IsPrimaryKey = true)] public int Id { get; set; } [DataField("MainId")] public int MainId { get; set; } [DataField("Data", IsNullable = true)] public string Data { get; set; }}public class TeUserAndExtend : TeUser{ [RelationField("Id", "MainId")] public TeUserExtend Extend { get; set; }}
var context = new DataContext();// query single datavar item = context.Query().Where(x => x.Id == 10).First();// query collection datasvar list = context.Query ().Where(x => x.Id > 10).ToList();// create datevar user = new TeUser() { Account = "foo", Password = "bar"};context.Insert(user);// update datauser.Password = "bar1";context.Update(user);// delete datacontext.Delete(user);
// basicvar list = context.Query() .Where (x => x.Id >= 5) .GroupBy (x => new LevelIdAgg () { LevelId = x.LevelId, Data = Function.Count () }) .ToList ();// date formatvar list = context.Query () .GroupBy (x => new RegDateFormatAgg () { RegDateFormat = x.RegTime.ToString("yyyy-MM-dd"), Data = Function.Count () }) .ToList ();
// inner joinvar join = context.Query() .Join ((x,y) => x.Id == y.Id);// aggregate data join entity table var join = context.Query () .GroupBy(x => new { MId = x.MId, Count = Function.Count(), }) .Join ((x, y) => x.MId == y.Id);
// basic parametervar sql = "update Te_User set NickName=@P2 where Id=@P1";var ps = new DataParameter[2];ps[0] = new DataParameter("P1", 5);ps[1] = new DataParameter("P2", "abc");var executor = context.CreateSqlStringExecutor(sql, ps);var ret = executor.ExecuteNonQuery();// object parametervar sql = "update Te_User set NickName={nickname} where Id={id}";var executor = context.CreateSqlStringExecutor(sql, new { nickname = "abc", id = 5 });var ret = executor.ExecuteNonQuery();
该项目使用xUnit进行单元测试,测试代码地址::
每个数据库有超过300组测试,包含1,000个案例,涵盖了大部分代码。
目前只在同一台计算机上使用EF Core,为Linux做Docker Sql Server 2017做简单的CURD性能测试代码地址
1000次CUD和单个数据查询
共有5轮,每轮1000次CUD和1000次数据查询
EF测试结果
Light.Data测试结果
从比较来看,查询性能几乎相同,插入性能Light.Data略胜一筹,批量更新也略胜一筹。
本文仅作简要介绍,您可以参考文档和测试用例以了解具体的使用方法。
原文地址:
转载地址:http://sizhj.baihongyu.com/