update

28810
2020-02-27 17:56:34 +08:00
parent abd17cec40
commit 46c352016a
23 changed files with 51 additions and 232 deletions

@@ -10,7 +10,7 @@ FreeSql是一个功能强大的NETStandard库用于对象关系映射程序(O
- [x] 支持导航属性查询,和延时加载;
- [x] 支持同步/异步数据库操作方法,丰富多彩的链式查询方法;
- [x] 支持读写分离、分表分库,租户设计;
- [x] 支持多种数据库MySql/SqlServer/PostgreSQL/Oracle/Sqlite/达梦;
- [x] 支持多种数据库MySql/SqlServer/PostgreSQL/Oracle/Sqlite/达梦/MsAccess
* [FreeSql入门](https://github.com/2881099/FreeSql/wiki/FreeSql%e5%85%a5%e9%97%a8)
* [安装](https://github.com/2881099/FreeSql/wiki/%e5%ae%89%e8%a3%85)
@@ -29,7 +29,6 @@ FreeSql是一个功能强大的NETStandard库用于对象关系映射程序(O
* [分页](https://github.com/2881099/FreeSql/wiki/%e5%88%86%e9%a1%b5%e6%9f%a5%e8%af%a2)
* [单表](https://github.com/2881099/FreeSql/wiki/%e5%8d%95%e8%a1%a8%e6%9f%a5%e8%af%a2)
* [多表](https://github.com/2881099/FreeSql/wiki/%e5%a4%9a%e8%a1%a8%e6%9f%a5%e8%af%a2)
* [利用导航属性](https://github.com/2881099/FreeSql/wiki/%e5%88%a9%e7%94%a8%e5%af%bc%e8%88%aa%e5%b1%9e%e6%80%a7)
* [分组聚合](https://github.com/2881099/FreeSql/wiki/%e5%88%86%e7%bb%84%e8%81%9a%e5%90%88%e6%9f%a5%e8%af%a2)
* [返回数据!!](https://github.com/2881099/FreeSql/wiki/%e8%bf%94%e5%9b%9e%e6%95%b0%e6%8d%ae)
* [延时加载](https://github.com/2881099/FreeSql/wiki/%e5%bb%b6%e6%97%b6%e5%8a%a0%e8%bd%bd)

@@ -147,7 +147,6 @@ var t1 = (
## 参考资料
- [《多表查询》](https://github.com/2881099/FreeSql/wiki/%e5%a4%9a%e8%a1%a8%e6%9f%a5%e8%af%a2)
- [《利用导航属性查询》](https://github.com/2881099/FreeSql/wiki/%e5%88%a9%e7%94%a8%e5%af%bc%e8%88%aa%e5%b1%9e%e6%80%a7)
- [《返回数据》](https://github.com/2881099/FreeSql/wiki/%e8%bf%94%e5%9b%9e%e6%95%b0%e6%8d%ae)
- [《仓储层Repository》](https://github.com/2881099/FreeSql/wiki/Repository)
- [《优化之:延时加载》](https://github.com/2881099/FreeSql/wiki/%e5%bb%b6%e6%97%b6%e5%8a%a0%e8%bd%bd)

@@ -17,7 +17,6 @@
* [分页](https://github.com/2881099/FreeSql/wiki/%e5%88%86%e9%a1%b5%e6%9f%a5%e8%af%a2)
* [单表](https://github.com/2881099/FreeSql/wiki/%e5%8d%95%e8%a1%a8%e6%9f%a5%e8%af%a2)
* [多表](https://github.com/2881099/FreeSql/wiki/%e5%a4%9a%e8%a1%a8%e6%9f%a5%e8%af%a2)
* [利用导航属性](https://github.com/2881099/FreeSql/wiki/%e5%88%a9%e7%94%a8%e5%af%bc%e8%88%aa%e5%b1%9e%e6%80%a7)
* [分组聚合](https://github.com/2881099/FreeSql/wiki/%e5%88%86%e7%bb%84%e8%81%9a%e5%90%88%e6%9f%a5%e8%af%a2)
* [返回数据!!](https://github.com/2881099/FreeSql/wiki/%e8%bf%94%e5%9b%9e%e6%95%b0%e6%8d%ae)
* [延时加载](https://github.com/2881099/FreeSql/wiki/%e5%bb%b6%e6%97%b6%e5%8a%a0%e8%bd%bd)

@@ -4,7 +4,7 @@ FreeSql支持丰富的更新数据方法支持单条或批量更新在特
var connstr = "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;" +
"Initial Catalog=cccddd;Charset=utf8;SslMode=none;Max pool size=10";
IFreeSql fsql = new FreeSql.FreeSqlBuilder()
static IFreeSql fsql = new FreeSql.FreeSqlBuilder()
.UseConnectionString(FreeSql.DataType.MySql, connstr)
.UseAutoSyncStructure(true) //自动同步实体结构到数据库
.Build(); //请务必定义成 Singleton 单例模式

@@ -38,7 +38,7 @@ create table fenbiao_2018 partition of fenbiao for values in ('2018');
数据库中的数据量不一定是可控的在未进行分库分表的情况下随着时间和业务的发展库中的表会越来越多表中的数据量也会越来越大相应地数据操作增删改查的开销也会越来越大另外一台服务器的资源CPU、磁盘、内存、IO等是有限的最终数据库所能承载的数据量、数据处理能力都将遭遇瓶颈。
## FreeSql.Repository 分表
## FreeSql.Repository 分表
FreeSql 提供 AsTable 分表的基础方法GuidRepository 作为分存式仓储将实现了分表与分库(不支持跨服务器分库)的封装。

@@ -1,5 +1,5 @@
```csharp
IFreeSql fsql = new FreeSql.FreeSqlBuilder()
static IFreeSql fsql = new FreeSql.FreeSqlBuilder()
.UseConnectionString(FreeSql.DataType.MySql, "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;Initial Catalog=cccddd;Charset=utf8;SslMode=none;Max pool size=10")
.Build(); //请务必定义成 Singleton 单例模式
@@ -55,7 +55,6 @@ var groupby = fsql.Select<Topic>()
## 参考资料
- [《多表查询》](https://github.com/2881099/FreeSql/wiki/%e5%a4%9a%e8%a1%a8%e6%9f%a5%e8%af%a2)
- [《利用导航属性查询》](https://github.com/2881099/FreeSql/wiki/%e5%88%a9%e7%94%a8%e5%af%bc%e8%88%aa%e5%b1%9e%e6%80%a7)
- [《返回数据》](https://github.com/2881099/FreeSql/wiki/%e8%bf%94%e5%9b%9e%e6%95%b0%e6%8d%ae)
- [《LinqToSql》](https://github.com/2881099/FreeSql/wiki/LinqToSql)
- [《仓储层Repository》](https://github.com/2881099/FreeSql/wiki/Repository)

@@ -1,5 +1,5 @@
```csharp
IFreeSql fsql = new FreeSql.FreeSqlBuilder()
static IFreeSql fsql = new FreeSql.FreeSqlBuilder()
.UseConnectionString(FreeSql.DataType.MySql, "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;Initial Catalog=cccddd;Charset=utf8;SslMode=none;Max pool size=10")
.Build(); //请务必定义成 Singleton 单例模式
@@ -51,7 +51,6 @@ SqlServer 2012+ 版本,使用最新的 fetch next rows 分页;
## 参考资料
- [《多表查询》](https://github.com/2881099/FreeSql/wiki/%e5%a4%9a%e8%a1%a8%e6%9f%a5%e8%af%a2)
- [《利用导航属性查询》](https://github.com/2881099/FreeSql/wiki/%e5%88%a9%e7%94%a8%e5%af%bc%e8%88%aa%e5%b1%9e%e6%80%a7)
- [《返回数据》](https://github.com/2881099/FreeSql/wiki/%e8%bf%94%e5%9b%9e%e6%95%b0%e6%8d%ae)
- [《LinqToSql》](https://github.com/2881099/FreeSql/wiki/LinqToSql)
- [《仓储层Repository》](https://github.com/2881099/FreeSql/wiki/Repository)

@@ -10,7 +10,7 @@
var connstr = "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;" +
"Initial Catalog=cccddd;Charset=utf8;SslMode=none;Max pool size=10";
IFreeSql fsql = new FreeSql.FreeSqlBuilder()
static IFreeSql fsql = new FreeSql.FreeSqlBuilder()
.UseConnectionString(FreeSql.DataType.MySql, connstr)
.UseAutoSyncStructure(true) //自动同步实体结构到数据库
.Build(); //请务必定义成 Singleton 单例模式

@@ -1,70 +0,0 @@
```csharp
IFreeSql fsql = new FreeSql.FreeSqlBuilder()
.UseConnectionString(FreeSql.DataType.MySql, "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;Initial Catalog=cccddd;Charset=utf8;SslMode=none;Max pool size=10")
.Build(); //请务必定义成 Singleton 单例模式
[Table(Name = "tb_topic")]
class Topic {
[Column(IsIdentity = true, IsPrimary = true)]
public int Id { get; set; }
public int Clicks { get; set; }
public int TestTypeInfoGuid { get; set; }
public TestTypeInfo Type { get; set; }
public string Title { get; set; }
public DateTime CreateTime { get; set; }
}
class TestTypeInfo {
public int Guid { get; set; }
public int ParentId { get; set; }
public TestTypeParentInfo Parent { get; set; }
public string Name { get; set; }
}
class TestTypeParentInfo {
public int Id { get; set; }
public string Name { get; set; }
}
ISelect<Topic> select => fsql.Select<Topic>();
```
### 联表,使用导航属性
```csharp
sql = select.LeftJoin(a => a.Type.Guid == a.TestTypeInfoGuid).ToSql();
//SELECT a.`Id`, a.`Clicks`, a.`TestTypeInfoGuid`, a__Type.`Guid`, a__Type.`ParentId`, a__Type.`Name`, a.`Title`, a.`CreateTime`
//FROM `tb_topic` a
//LEFT JOIN `TestTypeInfo` a__Type ON a__Type.`Guid` = a.`TestTypeInfoGuid`
sql = select
.LeftJoin(a => a.Type.Guid == a.TestTypeInfoGuid)
.LeftJoin(a => a.Type.Parent.Id == a.Type.ParentId).ToSql();
//SELECT a.`Id`, a.`Clicks`, a.`TestTypeInfoGuid`, a__Type.`Guid`, a__Type.`ParentId`, a__Type.`Name`, a.`Title`, a.`CreateTime`
//FROM `tb_topic` a
//LEFT JOIN `TestTypeInfo` a__Type ON a__Type.`Guid` = a.`TestTypeInfoGuid`
//LEFT JOIN `TestTypeParentInfo` a__Type__Parent ON a__Type__Parent.`Id` = a__Type.`ParentId`
```
## 多表,使用导航属性
```csharp
sql = select.Where(a => a.Type.Name == "typeTitle" && a.Type.Guid == a.TestTypeInfoGuid).ToSql();
///SELECT a.`Id`, a.`Clicks`, a.`TestTypeInfoGuid`, a.`Title`, a.`CreateTime`
//FROM `tb_topic` a, `TestTypeInfo` a__Type
//WHERE (a__Type.`Name` = 'typeTitle' AND a__Type.`Guid` = a.`TestTypeInfoGuid`)
sql = select.Where(a => a.Type.Parent.Name == "tparent").ToSql();
//SELECT a.`Id`, a.`Clicks`, a.`TestTypeInfoGuid`, a.`Title`, a.`CreateTime`
//FROM `tb_topic` a, `TestTypeInfo` a__Type, `TestTypeParentInfo` a__Type__Parent
//WHERE (a__Type__Parent.`Name` = 'tparent')
```
## 参考资料
- [《多表查询》](https://github.com/2881099/FreeSql/wiki/%e5%a4%9a%e8%a1%a8%e6%9f%a5%e8%af%a2)
- [《返回数据》](https://github.com/2881099/FreeSql/wiki/%e8%bf%94%e5%9b%9e%e6%95%b0%e6%8d%ae)
- [《仓储层Repository》](https://github.com/2881099/FreeSql/wiki/Repository)
- [《LinqToSql》](https://github.com/2881099/FreeSql/wiki/LinqToSql)
- [《优化之:延时加载》](https://github.com/2881099/FreeSql/wiki/%e5%bb%b6%e6%97%b6%e5%8a%a0%e8%bd%bd)
- [《优化之:贪婪加载》](https://github.com/2881099/FreeSql/wiki/%e8%b4%aa%e5%a9%aa%e5%8a%a0%e8%bd%bd)
- [《Expression 表达式函数》](https://github.com/2881099/FreeSql/wiki/%e8%a1%a8%e8%be%be%e5%bc%8f%e5%87%bd%e6%95%b0)
- [《性能》](https://github.com/2881099/FreeSql/wiki/%e6%80%a7%e8%83%bd)
- [《分区、分表、分库》](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)

@@ -45,7 +45,6 @@ var sql = fsql.Select<User>()
## 参考资料
- [《多表查询》](https://github.com/2881099/FreeSql/wiki/%e5%a4%9a%e8%a1%a8%e6%9f%a5%e8%af%a2)
- [《利用导航属性查询》](https://github.com/2881099/FreeSql/wiki/%e5%88%a9%e7%94%a8%e5%af%bc%e8%88%aa%e5%b1%9e%e6%80%a7)
- [《返回数据》](https://github.com/2881099/FreeSql/wiki/%e8%bf%94%e5%9b%9e%e6%95%b0%e6%8d%ae)
- [《LinqToSql》](https://github.com/2881099/FreeSql/wiki/LinqToSql)
- [《仓储层Repository》](https://github.com/2881099/FreeSql/wiki/Repository)

@@ -162,7 +162,6 @@ WHERE t1.IsDeleted = 0
## 参考资料
- [《利用导航属性查询》](https://github.com/2881099/FreeSql/wiki/%e5%88%a9%e7%94%a8%e5%af%bc%e8%88%aa%e5%b1%9e%e6%80%a7)
- [《返回数据》](https://github.com/2881099/FreeSql/wiki/%e8%bf%94%e5%9b%9e%e6%95%b0%e6%8d%ae)
- [《仓储层Repository》](https://github.com/2881099/FreeSql/wiki/Repository)
- [《LinqToSql》](https://github.com/2881099/FreeSql/wiki/LinqToSql)

@@ -36,7 +36,6 @@ FreeSql 除了支持基本的增删查改功能外,还支持基于现有数据
- [《使用读写分离》](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/%e5%88%a9%e7%94%a8%e5%af%bc%e8%88%aa%e5%b1%9e%e6%80%a7)
- [《返回数据》](https://github.com/2881099/FreeSql/wiki/%e8%bf%94%e5%9b%9e%e6%95%b0%e6%8d%ae)
- [《优化之:延时加载》](https://github.com/2881099/FreeSql/wiki/%e5%bb%b6%e6%97%b6%e5%8a%a0%e8%bd%bd)
- [《优化之:贪婪加载》](https://github.com/2881099/FreeSql/wiki/%e8%b4%aa%e5%a9%aa%e5%8a%a0%e8%bd%bd)

@@ -5,7 +5,7 @@ UnitOfWork 可将多个仓储放在一个单元管理执行,最终通用 Commi
var connstr = "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;" +
"Initial Catalog=cccddd;Charset=utf8;SslMode=none;Max pool size=10";
IFreeSql fsql = new FreeSql.FreeSqlBuilder()
static IFreeSql fsql = new FreeSql.FreeSqlBuilder()
.UseConnectionString(FreeSql.DataType.MySql, connstr)
.UseAutoSyncStructure(true) //自动同步实体结构到数据库
.Build(); //请务必定义成 Singleton 单例模式

@@ -120,7 +120,6 @@ limit 0,1))
## 参考资料
- [《多表查询》](https://github.com/2881099/FreeSql/wiki/%e5%a4%9a%e8%a1%a8%e6%9f%a5%e8%af%a2)
- [《利用导航属性查询》](https://github.com/2881099/FreeSql/wiki/%e5%88%a9%e7%94%a8%e5%af%bc%e8%88%aa%e5%b1%9e%e6%80%a7)
- [《返回数据》](https://github.com/2881099/FreeSql/wiki/%e8%bf%94%e5%9b%9e%e6%95%b0%e6%8d%ae)
- [《仓储层Repository》](https://github.com/2881099/FreeSql/wiki/Repository)
- [《过滤器、全局过滤器》](https://github.com/2881099/FreeSql/wiki/%e8%bf%87%e6%bb%a4%e5%99%a8)

@@ -208,7 +208,6 @@ public void QueryList() {
- [《使用读写分离》](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/%e5%88%a9%e7%94%a8%e5%af%bc%e8%88%aa%e5%b1%9e%e6%80%a7)
- [《返回数据》](https://github.com/2881099/FreeSql/wiki/%e8%bf%94%e5%9b%9e%e6%95%b0%e6%8d%ae)
- [《仓储层Repository》](https://github.com/2881099/FreeSql/wiki/Repository)
- [《过滤器、全局过滤器》](https://github.com/2881099/FreeSql/wiki/%e8%bf%87%e6%bb%a4%e5%99%a8)

@@ -3,7 +3,6 @@ FreeSql在查询数据下足了功能链式查询语法、多表查询、表
- [《分页查询》](https://github.com/2881099/FreeSql/wiki/%e5%88%86%e9%a1%b5%e6%9f%a5%e8%af%a2)
- [《单表查询》](https://github.com/2881099/FreeSql/wiki/%e5%8d%95%e8%a1%a8%e6%9f%a5%e8%af%a2)
- [《多表查询》](https://github.com/2881099/FreeSql/wiki/%e5%a4%9a%e8%a1%a8%e6%9f%a5%e8%af%a2)
- [《利用导航属性查询》](https://github.com/2881099/FreeSql/wiki/%e5%88%a9%e7%94%a8%e5%af%bc%e8%88%aa%e5%b1%9e%e6%80%a7)
- [《分组聚合查询》](https://github.com/2881099/FreeSql/wiki/%e5%88%86%e7%bb%84%e8%81%9a%e5%90%88%e6%9f%a5%e8%af%a2)
- [《返回数据》](https://github.com/2881099/FreeSql/wiki/%e8%bf%94%e5%9b%9e%e6%95%b0%e6%8d%ae)
- [《LinqToSql》](https://github.com/2881099/FreeSql/wiki/LinqToSql)

@@ -4,7 +4,7 @@ FreeSql 提供单条和批量插入数据的方法,在特定的数据库执行
var connstr = "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;" +
"Initial Catalog=cccddd;Charset=utf8;SslMode=none;Max pool size=10";
IFreeSql fsql = new FreeSql.FreeSqlBuilder()
static IFreeSql fsql = new FreeSql.FreeSqlBuilder()
.UseConnectionString(FreeSql.DataType.MySql, connstr)
.UseAutoSyncStructure(true) //自动同步实体结构到数据库
.Build(); //请务必定义成 Singleton 单例模式

@@ -1,49 +0,0 @@
FreeSql增加了缓存支持可以快速将查询数据缓存起来以便提升性能。
```csharp
IFreeSql fsql = new FreeSql.FreeSqlBuilder()
.UseConnectionString(FreeSql.DataType.MySql, "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;Initial Catalog=cccddd;Charset=utf8;SslMode=none;Max pool size=10")
.UseCache(null) //使用缓存,不指定默认使用内存 IDistributedCache
.Build(); //请务必定义成 Singleton 单例模式
[Table(Name = "tb_topic")]
class Topic {
[Column(IsIdentity = true, IsPrimary = true)]
public int Id { get; set; }
public int Clicks { get; set; }
public int TestTypeInfoGuid { get; set; }
public string Title { get; set; }
public DateTime CreateTime { get; set; }
}
ISelect<Topic> select => fsql.Select<Topic>();
var result1 = select.Where(a => 1 == 1).Caching(20, "testcaching").ToList(); //结果被缓存起来
var testcaching1 = g.mysql.Cache.Get("testcaching"); //这里可以查看缓存的值
Assert.NotNull(testcaching1);
var result2 = select.Where(a => 1 == 1).Caching(20, "testcaching").ToList(); //这次查询使用了缓存
var testcaching2 = g.mysql.Cache.Get("testcaching");
Assert.NotNull(testcaching2);
Assert.Equal(result1.Count, result1.Count);
```
## 参考资料
* [《CodeFirst模式开发介绍》](https://github.com/2881099/FreeSql/wiki/CodeFirst)
* [《CodeFirst模式之一实体特性》](https://github.com/2881099/FreeSql/wiki/%e5%ae%9e%e4%bd%93%e7%89%b9%e6%80%a7)
* [《CodeFirst模式之二FluentApi》](https://github.com/2881099/FreeSql/wiki/FluentApi)
* [《CodeFirst模式之三自定义特性》](https://github.com/2881099/FreeSql/wiki/%e8%87%aa%e5%ae%9a%e4%b9%89%e7%89%b9%e6%80%a7)
* [《CodeFirst模式之四类型映射》](https://github.com/2881099/FreeSql/wiki/%e7%b1%bb%e5%9e%8b%e6%98%a0%e5%b0%84)
* [《CodeFirst模式之五迁移结构》](https://github.com/2881099/FreeSql/wiki/CodeFirst#%e8%bf%81%e7%a7%bb%e7%bb%93%e6%9e%84)
- [《DbFirst模式开发介绍》](https://github.com/2881099/FreeSql/wiki/DbFirst)
- [《使用读写分离》](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/%e8%bf%87%e6%bb%a4%e5%99%a8)
- [《租户》](https://github.com/2881099/FreeSql/wiki/%e7%a7%9f%e6%88%b7)
## API
| 方法 | 返回值 | 参数 | 描述 |
| ------------- | - | - | - |
| ToList | List\<T1\> | | 执行SQL查询返回 T1 实体所有字段的记录,若存在导航属性则一起查询返回,记录不存在时返回 Count 为 0 的列表 |
| 【其他】 |
| Caching | \<this\> | int seconds, string key = null | 缓存查询结果 |

@@ -14,7 +14,7 @@
# 关闭联级保存功能
全局关闭:
全局关闭(默认)
```csharp
fsql.SetDbContextOptions(opt => opt.EnableAddOrUpdateNavigateList = false);
@@ -26,7 +26,7 @@ var repo = fsql.GetRepository<T>();
repo.DbContextOptions.EnableAddOrUpdateNavigateList = false;
```
# SaveMany
# SaveMany(推荐使用)
保存实体的指定【一对多】、【多对多】导航属性SaveMany 方法实现在 BaseRepository、DbContext。

@@ -351,7 +351,6 @@ public static class DbFunc
## 参考资料
- [《多表查询》](https://github.com/2881099/FreeSql/wiki/%e5%a4%9a%e8%a1%a8%e6%9f%a5%e8%af%a2)
- [《利用导航属性查询》](https://github.com/2881099/FreeSql/wiki/%e5%88%a9%e7%94%a8%e5%af%bc%e8%88%aa%e5%b1%9e%e6%80%a7)
- [《返回数据》](https://github.com/2881099/FreeSql/wiki/%e8%bf%94%e5%9b%9e%e6%95%b0%e6%8d%ae)
- [《LinqToSql》](https://github.com/2881099/FreeSql/wiki/LinqToSql)
- [《仓储层Repository》](https://github.com/2881099/FreeSql/wiki/Repository)

@@ -1,4 +1,4 @@
## Dto 映射查询
## 1、Dto 映射查询
```csharp
Select<Tag>().Limit(10).ToList(a => new TestDto());
@@ -42,7 +42,7 @@ A, B, C 都有 idDto { id, a1, a2, b1, b2 }A.id 被映射。也可以指
> 友情提醒:在 dto 可以直接映射一个导航属性
## 导航属性 ManyToOne/OneToOne
## 2、导航属性 ManyToOne/OneToOne
ManyToOne/OneToOne 导航属性通过 ToList() 加载这个方法有一个参数includeNestedMembers。
@@ -67,7 +67,7 @@ Select<Tag>().Where(a => a.Parent.Name == "1").ToList();
Select<Tag>().Include(a => a.Parent).ToList();
```
## 导航属性 OneToMany/ManyToMany
## 3、导航属性 OneToMany/ManyToMany
IncludeMany 贪婪加载集合的导航属性,其实是分两次查询,在 ToList 后进行了数据重装。
@@ -87,7 +87,7 @@ Select<Tag>().IncludeMany(a => a.Songs,
然后,其实在 then 那里,还可以继续进行向下 Include/IncludeMany。只要你喜欢向下 100 层都没问题。
## 变异
## 4、变异
变异的 IncludeMany即使选择的不是导航属性也可以贪婪加载。
@@ -109,7 +109,7 @@ Select<Tag>().IncludeMany(a => a.TestManys.Take(10));
Select<Tag>().IncludeMany(a => a.TestManys.Select(b => new TestMany { Title = b.Title ... }));
```
## IncludeMany 扩展方法
## 5、IncludeMany 扩展方法
前面介绍 ISelect 中进行贪婪加载集合属性数据,主数据与子数据查询必须在一个代码逻辑内完成。
@@ -121,7 +121,7 @@ new List<Song>(new[] { song1, song2, song3 }).IncludeMany(fsql, a => a.Tags);
这是一个扩展方法IncludeMany方法名与 ISelect.IncludeMany 同名,参数基本一致(除了需要额外传递 IFreeSql 对象参数),功能也一致(包括前面提到的变异)。
## IncludeMany 两种方式对比
## 6、IncludeMany 两种方式对比
方式一IncludeMany 扩展方法):
@@ -191,7 +191,6 @@ dto.IncludeMany(d => d.Vods.Take(10).Where(vod => vod.TypeId == d.TypeId));
## 参考资料
- [《多表查询》](https://github.com/2881099/FreeSql/wiki/%e5%a4%9a%e8%a1%a8%e6%9f%a5%e8%af%a2)
- [《利用导航属性查询》](https://github.com/2881099/FreeSql/wiki/%e5%88%a9%e7%94%a8%e5%af%bc%e8%88%aa%e5%b1%9e%e6%80%a7)
- [《返回数据》](https://github.com/2881099/FreeSql/wiki/%e8%bf%94%e5%9b%9e%e6%95%b0%e6%8d%ae)
- [《LinqToSql》](https://github.com/2881099/FreeSql/wiki/LinqToSql)
- [《仓储层Repository》](https://github.com/2881099/FreeSql/wiki/Repository)

@@ -24,11 +24,13 @@ fsql.Select<TestAddEnum>().DisableGlobalFilter().ToList(); //禁用所有
fsql.Update/Delete 方法效果同上。
# 仓储过滤器
# 仓储过滤器(早期功能,可跳过以下内容)
FreeSql.Repository 也同样实现了过滤器功能,它不仅在查询时过滤,删除/修改/插入篇还会进行验证数据,避免数据安全问题。
FreeSql.Repository 也实现了过滤器功能,它在查询时过滤,删除/修改/插入篇还会进行验证数据,避免数据安全问题。
> 注意:仓储的过滤器与 IFreeSql.GlobalFilter 不是一个功能,可以同时生效
> 注意:仓储的过滤器与 IFreeSql.GlobalFilter 不是一个功能,可以同时生效
> 推荐使用 IFreeSql.GlobalFilter。仓储过滤器在早期出的功能会一直保留。
每个仓储实例都有 IDataFilter 属性,可利用其完成过滤器管理,它是独立的修改后不影响全局。

@@ -1,105 +1,55 @@
FreeSql 采用 ExpressionTree 优化读取速读,如果懂技术的你一定知道 .NETCore 技术下除了原生代码,最快就是 Emit 和 ExpressionTree。项目在初期使用的反射+缓存,虽然 .NETCore 优化了反射性能但经过与Dapper性能测试对比之后发现仍然有一定差距改成 ExpresstionTree 后才与 Dapper 性能相当。FreeSql 支持的类型较多,实现 ExpressionTree 的复杂度较大,有兴趣的朋友可以翻阅源代码。
## 1、返回单条记录
```csharp
IFreeSql fsql = new FreeSql.FreeSqlBuilder()
.UseConnectionString(FreeSql.DataType.MySql, "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;Initial Catalog=cccddd;Charset=utf8;SslMode=none;Max pool size=10")
.Build(); //请务必定义成 Singleton 单例模式
[Table(Name = "tb_topic")]
class Topic {
[Column(IsIdentity = true, IsPrimary = true)]
public int Id { get; set; }
public int Clicks { get; set; }
public int TestTypeInfoGuid { get; set; }
public TestTypeInfo Type { get; set; }
public string Title { get; set; }
public DateTime CreateTime { get; set; }
}
class TestTypeInfo {
public int Guid { get; set; }
public int ParentId { get; set; }
public TestTypeParentInfo Parent { get; set; }
public string Name { get; set; }
}
class TestTypeParentInfo {
public int Id { get; set; }
public string Name { get; set; }
}
ISelect<Topic> select => fsql.Select<Topic>();
```
## 返回单条记录
```csharp
Topic t1 = select.Where(a => a.Id == 1).ToOne();
Topic t1 = fsql.Select<Topic>().ToOne();
```
> FreeSql有一个约定ToOne 永远返回 null 或 有数据的实体对象ToList 永远返回非 null 的 List\<实体类型\>
## 返回 List
## 2、返回 List
```csharp
List<Topic> t1 = select.Where(a => a.Id > 0).Skip(100).Limit(200).ToList();
List<Topic> t1 = fsql.Select<Topic>().ToList();
```
## 返回 List + 导航属性的数据
## 3、返回 List + 导航属性的数据
```csharp
List<Topic> t2 = select.LeftJoin(a => a.Type.Guid == a.TestTypeInfoGuid).ToList();
List<Topic> t2 = fsql.Select<Topic>().LeftJoin(a => a.Type.Id == a.TypeId).ToList();
//此时会返回普通字段 + 导航对象 Type 的数据
```
## 指定字段返回
## 4、指定字段返回
```csharp
//返回一个字段
List<int> t3 = select.Where(a => a.Id > 0).Skip(100).Limit(200).ToList(a => a.Id);
List<int> t3 = fsql.Select<Topic>().ToList(a => a.Id);
//返回匿名类
List<匿名类> t4 = select.Where(a => a.Id > 0).Skip(100).Limit(200).ToList(a => new { a.Id, a.Title });
List<匿名类> t4 = fsql.Select<Topic>().ToList(a => new { a.Id, a.Title });
//返回元组
List<(int, string)> t5 = select.Where(a => a.Id > 0).Skip(100).Limit(200).ToList<(int, string)>("id, title");
List<(int, string)> t5 = fsql.Select<Topic>().ToList<(int, string)>("id, title");
//返回SQL字段
List<匿名类> t6 = select.Where(a => a.Id > 0).Skip(100).Limit(200)
.ToList(a => new {
a.Id,
a.Title,
cstitle = "substr(a.title, 0, 2)", //将 substr(a.title, 0, 2) 作为查询字段
csnow = Convert.ToDateTime("now()"), //将 now() 作为查询字段
//奇思妙想:怎么查询开窗函数的结果
});
//返回更为复杂的结构
List<匿名类> t7 = select.From<TestTypeInfo, TestTypeParentInfo>((s, b, c) => s
.InnerJoin(a => a.TypeGuid == b.Guid)
.LeftJoin(a => c.Id == b.ParentId)
.Where(a => b.Name != "xxx")).ToList((a, b, c) => new {
a.Id,
ccc = new { a.Id, a.Title },
tp = a.Type,
tp2 = new {
a.Id,
tp2 = a.Type.Name
},
tp3 = new {
a.Id,
tp33 = new {
a.Id
}
}
});
List<匿名类> t6 = fsql.Select<Topic>().ToList(a => new {
a.Id,
a.Title,
a.Type, //可以直接返回导航属性 Type
cstitle = "substr(a.title, 0, 2)", //将 substr(a.title, 0, 2) 作为查询字段
csnow = Convert.ToDateTime("now()"), //将 now() 作为查询字段
//奇思妙想:怎么查询开窗函数的结果
});
//返回子查询的字段
List<匿名类> t6 = select.Where(a => a.Id > 0).Skip(100).Limit(200)
.ToList(a => new {
a.Id,
count = fsql.Select<T2>().Count(),
max = fsql.Select<T2>().Max(b => b.Id),
min = fsql.Select<T2>().Min(b => b.Id),
name = fsql.Select<2>().First(b => b.name)
});
List<匿名类> t6 = fsql.Select<Topic>().ToList(a => new {
a.Id,
count = fsql.Select<T2>().Count(),
max = fsql.Select<T2>().Max(b => b.Id),
min = fsql.Select<T2>().Min(b => b.Id),
name = fsql.Select<2>().First(b => b.name)
});
```
### 执行SQL
### 5、执行SQL
```csharp
class xxx {
public int Id { get; set; }
@@ -112,7 +62,7 @@ List<(int, string ,string)> t7 = fsql.Ado.Query<(int, string, string)>("select *
List<dynamic> t9 = fsql.Ado.Query<dynamic>("select * from song");
```
## 巧用AsTable
## 6、巧用AsTable
```csharp
var sql = fsql.Select<User>()
.AsTable((a, b) => "(select * from tb_topic where clicks > 10)")
@@ -120,7 +70,7 @@ var sql = fsql.Select<User>()
```
> v1.0.1 ISelect 增加 WithSql("select * from user ...") 功能快速代理此方法
## ToChunk
## 7、ToChunk
执行查询分块返回数据可减少内存开销。比如读取10万条数据每次返回100条处理。
```csharp
@@ -133,7 +83,7 @@ fsql.Select<Song>().OrderBy(a => a.Id).ToChunk(100, list =>
//这里示范,最终 testlist1 与 testlist2 返回的数据相同。
```
## Dto 映射查询
## 8、Dto 映射查询
```csharp
Select<Tag>().Limit(10).ToList(a => new TestDto());
@@ -177,7 +127,7 @@ A, B, C 都有 idDto { id, a1, a2, b1, b2 }A.id 被映射。也可以指
> 友情提醒:在 dto 可以直接映射一个导航属性
## API
## 9、API
| 方法 | 返回值 | 参数 | 描述 |
| ------------- | - | - | - |
@@ -196,7 +146,6 @@ A, B, C 都有 idDto { id, a1, a2, b1, b2 }A.id 被映射。也可以指
## 参考资料
- [《多表查询》](https://github.com/2881099/FreeSql/wiki/%e5%a4%9a%e8%a1%a8%e6%9f%a5%e8%af%a2)
- [《利用导航属性查询》](https://github.com/2881099/FreeSql/wiki/%e5%88%a9%e7%94%a8%e5%af%bc%e8%88%aa%e5%b1%9e%e6%80%a7)
- [《返回数据》](https://github.com/2881099/FreeSql/wiki/%e8%bf%94%e5%9b%9e%e6%95%b0%e6%8d%ae)
- [《LinqToSql》](https://github.com/2881099/FreeSql/wiki/LinqToSql)
- [《仓储层Repository》](https://github.com/2881099/FreeSql/wiki/Repository)