FreeSql
FreeSql 是一个功能强大的 NETStandard 库,用于对象关系映射程序(O/RM),提供了
CodeFirst/DbFirst/CURD/表达式函数/读写分离 等基础封装。支持 .NETCore 2.1+ 或 .NETFramework 4.6.1+。
新的查询功能
且先看看实体定义:
public class Song { [Column(IsIdentity = true)] public int Id { get; set; }
public DateTime? Create_time { get; set; } public bool? Is_deleted { get; set;
} public string Title { get; set; } public string Url { get; set; } public
virtual ICollection<Tag> Tags { get; set; } } public class Song_tag { public
int Song_id { get; set; } public virtual Song Song { get; set; } public int
Tag_id { get; set; } public virtual Tag Tag { get; set; } } public class Tag {
[Column(IsIdentity = true)] public int Id { get; set; } public int? Parent_id {
get; set; } public virtual Tag Parent { get; set; } public decimal? Ddd { get;
set; } public string Name { get; set; } public virtual ICollection<Song> Songs
{ get; set; } public virtual ICollection<Tag> Tags { get; set; } }
以上定义了三个实体,Song、Tag,以及中间表SongTag。
一对一、多对一的查询:
var t0 = fsql.Select<Tag>().Where(a => a.Parent.Parent.Name == "粤语").ToSql();
执行转换的SQL语句:
SELECT a.`Id`, a.`Parent_id`, a__Parent.`Id` as3, a__Parent.`Parent_id` as4,
a__Parent.`Ddd`, a__Parent.`Name`, a.`Ddd` as7, a.`Name` as8 FROM `Tag` a LEFT
JOIN `Tag` a__Parent ON a__Parent.`Id` = a.`Parent_id` LEFT JOIN `Tag`
a__Parent__Parent ON a__Parent__Parent.`Id` = a__Parent.`Parent_id` WHERE
(a__Parent__Parent.`Name` = '粤语')
一对多的查询:
var t1 = fsql.Select<Tag>().Where(a => a.Tags.AsSelect().Any(t => t.Parent.Id
== 10)).ToSql();
执行转换的SQL语句:
SELECT a.`Id`, a.`Parent_id`, a.`Ddd`, a.`Name` FROM `Tag` a WHERE
(exists(SELECT 1 FROM `Tag` t LEFT JOIN `Tag` t__Parent ON t__Parent.`Id` =
t.`Parent_id` WHERE (t__Parent.`Id` = 10) AND (t.`Parent_id` = a.`Id`) limit
0,1))
多对多的查询:
var t2 = fsql.Select<Song>().Where(s => s.Tags.AsSelect().Any(t => t.Name ==
"国语")).ToSql();
执行转换的SQL语句:
SELECT a.`Id`, a.`Create_time`, a.`Is_deleted`, a.`Title`, a.`Url` FROM `Song`
a WHERE(exists(SELECT 1 FROM `Song_tag` Mt_Ms WHERE(Mt_Ms.`Song_id` = a.`Id`)
AND(exists(SELECT 1 FROM `Tag` t WHERE(t.`Name` = '国语') AND(t.`Id` =
Mt_Ms.`Tag_id`) limit 0, 1)) limit 0, 1))
这个功能不受外建影响,更多前往wiki:《Select查询数据文档》
<https://github.com/2881099/FreeSql/wiki/%e6%9f%a5%e8%af%a2>
表达式函数
var t1 = select.Where(a => new[] { 1, 2, 3
}.Contains(a.testFieldInt)).ToSql(); //SELECT a.`Id`, a.`Clicks`,
a.`TestTypeInfoGuid`, a.`Title`, a.`CreateTime` //FROM `Song` a //WHERE (a.`Id`
in (1,2,3))
查找今天创建的数据
var t2 = select.Where(a => a.CreateTime.Date == DateTime.Now.Date).ToSql();
SqlServer 下随机获取记录
var t3 = select.OrderBy(a => Guid.NewGuid()).Limit(1).ToSql(); //SELECT top 1
... //FROM [Song] a //ORDER BY newid()
更多前往wiki:《Expression 表达式函数文档》
<https://github.com/2881099/FreeSql/wiki/%e8%a1%a8%e8%be%be%e5%bc%8f%e5%87%bd%e6%95%b0>
完整特性
* 支持 CodeFirst 迁移;
* 支持 DbFirst 从数据库导入实体类,支持三种模板生成器;
* 采用 ExpressionTree 高性能读取数据;
* 支持深入的类型映射,比如pgsql的数组类型,堪称匠心制作;
* 支持丰富的表达式函数;
* 支持导航属性查询,和延时加载;
* 支持同步/异步数据库操作方法,丰富多彩的链式查询方法;
* 支持读写分离、分表分库,租户设计;
* 支持多种数据库,MySql/SqlServer/PostgreSQL/Oracle/Sqlite;
入门 《Select》 <https://github.com/2881099/FreeSql/wiki/%e6%9f%a5%e8%af%a2> |
《Update》 <https://github.com/2881099/FreeSql/wiki/%e4%bf%ae%e6%94%b9> | 《Insert》
<https://github.com/2881099/FreeSql/wiki/%e6%b7%bb%e5%8a%a0> | 《Delete》
<https://github.com/2881099/FreeSql/wiki/%e5%88%a0%e9%99%a4>
新手 《表达式函数》
<https://github.com/2881099/FreeSql/wiki/%e8%a1%a8%e8%be%be%e5%bc%8f%e5%87%bd%e6%95%b0>
|《CodeFirst》 <https://github.com/2881099/FreeSql/wiki/CodeFirst> | 《DbFirst》
<https://github.com/2881099/FreeSql/wiki/DbFirst>
高手 《Repository》 <https://github.com/2881099/FreeSql/wiki/Repository> |
《UnitOfWork》
<https://github.com/2881099/FreeSql/wiki/%e5%b7%a5%e4%bd%9c%e5%8d%95%e5%85%83> |
《过滤器》 <https://github.com/2881099/FreeSql/wiki/%e8%bf%87%e6%bb%a4%e5%99%a8>
不朽 《读写分离》
<https://github.com/2881099/FreeSql/wiki/%e8%af%bb%e5%86%99%e5%88%86%e7%a6%bb> |
《分区分表》
<https://github.com/2881099/FreeSql/wiki/%e5%88%86%e5%8c%ba%e5%88%86%e8%a1%a8> |
《租户》 <https://github.com/2881099/FreeSql/wiki/%e7%a7%9f%e6%88%b7> | 更新日志
<https://github.com/2881099/FreeSql/wiki/%e6%9b%b4%e6%96%b0%e6%97%a5%e5%bf%97>
快速开始
以 .net core 新项目为例,创建新项目
dotnet new webapi
引入 FreeSql 包
dotnet add package FreeSql.Repository
在 startup.cs 中定义 IFreeSql 和注入仓储
public Startup(IConfiguration configuration, ILoggerFactory loggerFactory) {
Configuration = configuration; Fsql = new FreeSql.FreeSqlBuilder()
.UseConnectionString(FreeSql.DataType.Sqlite, @"Data
Source=|DataDirectory|/document.db;Pooling=true;Max Pool Size=10")
.UseAutoSyncStructure(true) //自动同步实体结构到数据库 .UseLazyLoading(true) //开启延时加载,导航属性
.UseMonitorCommand(cmd => Trace.WriteLine(cmd.CommandText)) //跟踪SQL执行语句
.Build(); } public IConfiguration Configuration { get; } public IFreeSql Fsql {
get; } public IServiceProvider ConfigureServices(IServiceCollection services) {
services.AddMvc(); services.AddSingleton<IFreeSql>(Fsql); var builder = new
ContainerBuilder(); builder.RegisterFreeRepository( filter => filter
.Apply<ISoftDelete>("softdelete", a => a.IsDeleted == false)
//开启软删除过滤器,可定义多个全局过滤器 , this.GetType().Assembly //将本项目中所有继承实现的仓储批量注入 );
builder.Populate(services); var container = builder.Build(); return new
AutofacServiceProvider(container); }
然后在 controller 中就可以像平常一样使用仓储了,如:
[Route("restapi/[controller]")] public class SongsController : Controller {
GuidRepository<Song> _songRepository; public
SongsController(GuidRepository<Song> repos1) { _songRepository = repos1; }
FreeSql.Repository
FreeSql.Repository 参考 abp vnext
接口,定义和实现基础的仓储层(CURD)。除此以外,它还实用的全局、局部过滤器功能,分表分方库功能,以及工作单元的实现;
过滤器功能不仅可以查询时过滤,连删除/修改/插入时都会进行验证,避免开过过程担心数据安全问题;
UnitOfWork 可将多个仓储放在一个单元管理执行,最终通用 Commit 执行所有操作,内部采用了数据库事务;
结束语
本次更新主要涉及 一对一、多对一、一对多、多对多 的查询,当约定配置不正确的时候使用导航属性,会出现友好的错误提示。
感谢您的关注,github:https://github.com/2881099/FreeSql
<https://github.com/2881099/FreeSql>,求给出宝贵的一星,谢谢!
热门工具 换一换