FluentData
作为一名老程序猿,用过的ORM也不少,真的用起来小巧轻盈顺手的还是FluentData,可能是习惯了(每个人习惯的力量很大)。
大部分ORM在定义entity时,都加了些自定义注解,和使用的ORM强耦合。
在尽量少入侵的情况下,fork了大佬的FluentData 在此基础上丰富了一点内容,形成 FluentDataStd :standard2.0
using DbContextIns dbContext = new DbContextIns(); Expression<Func<Test, bool>> filter = (i) => i.StagePrice > 100; filter = filter.And((i) => "中国上海".StartsWith(i.Name) && i.Name != null); filter = filter.Or((i) => i.Name == "66'" && i.Qty == null); Test test1 = dbContext.GetSingle(filter); test1.Qty += 1; test1.Current = DateTime.Now; dbContext.UpdateColumns(test1, new List<Expression<Func<Test, object>>>() { (i) => i.Qty, (i) => i.Current }, (i) => i.Id, (i) => i.Name); dbContext.Update(test1).Columns((i) => i.Qty, (i) => i.Current).WhereFields((i) => i.Id, (i) => i.Name).Execute();//等同上面 UpdateColumns //update Test set `Qty` = @Qty, `Current` = @Current where `Id` = @Id and `Name` = @Name dbContext.UpdateIgnoreColumns(test1, new List<Expression<Func<Test, object>>>() { (i) => i.Qty }, (i) => i.Id); dbContext.Update(test1).AutoMap((i) => i.Qty, (i) => i.Id).WhereFields((i) => i.Id).Execute();//等同上面 UpdateIgnoreColumns //update Test set `Name` = @Name, `Current` = @Current, `StagePrice` = @StagePrice, `Status` = @Status where `Id` = @Id dbContext.UpdateAllColumns(test1, (i) => i.Id); //update Test set `Name` = @Name, `Current` = @Current, `StagePrice` = @StagePrice, `Qty` = @Qty, `Status` = @Status where `Id` = @Id List<Test> tests = dbContext.GetMany(filter, 1, 10, (i) => i.Id, true);//第1页,每页10条,Id升序,filter过滤 // SELECT * FROM Test WHERE (((StagePrice > 100) AND (('中国上海' LIKE CONCAT('',Name,'%')) AND (Name IS NOT null))) OR ((Name = '66''') AND (Qty IS null))) ORDER BY Id LIMIT 0, 10 Test testIns = new Test() { Name = "test", Qty = 12, Id = 5, StagePrice = 6 }; long autoId = dbContext.InsertGetAutoIdIgnoreColumns(testIns, (i) => i.Id, (i) => i.Qty);//Id自增,忽略Qty字段 //insert into Test(`Name`,`Current`,`StagePrice`,`Status`) values(@Name,@Current,@StagePrice,@Status);select LAST_INSERT_ID() as `LastInsertedId` dbContext.InsertIgnoreColumns(testIns, (i) => i.StagePrice);//忽略 StagePrice字段 //insert into Test(`Id`,`Name`,`Current`,`Qty`,`Status`) values(@Id,@Name,@Current,@Qty,@Status) dbContext.InsertAllColumns(testIns); //insert into Test(`Id`,`Name`,`Current`,`StagePrice`,`Qty`,`Status`) values(@Id,@Name,@Current,@StagePrice,@Qty,@Status) long autoId2 = dbContext.InsertAllColumns(testIns, (i) => i.Id);//插入全部列,返回自增Id //insert into Test(`Name`,`Current`,`StagePrice`,`Qty`,`Status`) values(@Name,@Current,@StagePrice,@Qty,@Status);select LAST_INSERT_ID() as `LastInsertedId` dbContext.DeleteByFields(testIns, (i) => i.Id, (i) => i.Name); //delete from Test where `Id` = @Id and `Name` = @Name
public class DbContextIns : FluentDataStd.DbContext { public DbContextIns() { ConnectionString("Data Source=localhost;port=3306;Initial Catalog=test;user id=root;password=root;charset=utf8mb4;SslMode=None", new FluentDataStd.MySqlProvider(), MySql.Data.MySqlClient.MySqlClientFactory.Instance); this.OnExecuting((i) => { Console.WriteLine(i.Command.CommandText); }); this.IgnoreIfAutoMapFails(true); } }
public class Test { public int Id { get; set; } public string Name { get; set; } public DateTime? Current { get; set; } public decimal? StagePrice { get; set; } public int? Qty { get; set; } public StatusEnum Status { get; set; } } public enum StatusEnum { [Description("正常")] Normal = 1, [Description("删除")] Deleted = 2, [Description("异常")] Exception = 500, }
https://www.nuget.org/packages/FluentDataStd/5.0.2023.306
posted on 2023-04-06 13:23 jonney_wang 阅读(165) 评论(0) 收藏 举报