From 882e671eee0e4ec865d61f9ebbe77c4e4513f8b7 Mon Sep 17 00:00:00 2001 From: 28810 <28810@YEXIANGQIN> Date: Wed, 12 Aug 2020 04:11:22 +0800 Subject: [PATCH] =?UTF-8?q?-=20=E4=BC=98=E5=8C=96=20=E6=97=A0=E4=B8=BB?= =?UTF-8?q?=E9=94=AE=E4=BD=BF=E7=94=A8=20IUpdate.SetSource=20=E7=9A=84?= =?UTF-8?q?=E9=94=99=E8=AF=AF=E6=8F=90=E7=A4=BA=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FreeSql.DbContext/FreeSql.DbContext.xml | 16 ++ FreeSql.Tests/FreeSql.Tests/UnitTest3.cs | 7 + FreeSql/FreeSql.xml | 157 +----------------- FreeSql/Interface/Curd/IUpdate.cs | 6 +- .../Internal/CommonProvider/UpdateProvider.cs | 3 + 5 files changed, 34 insertions(+), 155 deletions(-) diff --git a/FreeSql.DbContext/FreeSql.DbContext.xml b/FreeSql.DbContext/FreeSql.DbContext.xml index 5ca74890..2acb6679 100644 --- a/FreeSql.DbContext/FreeSql.DbContext.xml +++ b/FreeSql.DbContext/FreeSql.DbContext.xml @@ -130,6 +130,13 @@ 清空状态数据 + + + 根据 lambda 条件删除数据 + + + + 添加 @@ -520,5 +527,14 @@ + + + 批量注入 Repository,可以参考代码自行调整 + + + + + + diff --git a/FreeSql.Tests/FreeSql.Tests/UnitTest3.cs b/FreeSql.Tests/FreeSql.Tests/UnitTest3.cs index d89e4db3..4693b4fb 100644 --- a/FreeSql.Tests/FreeSql.Tests/UnitTest3.cs +++ b/FreeSql.Tests/FreeSql.Tests/UnitTest3.cs @@ -169,9 +169,16 @@ namespace FreeSql.Tests public int int2 { get; set; } } + class testUpdateNonePk + { + public string name { get; set; } + } + [Fact] public void Test03() { + Assert.Throws(() => g.sqlite.Update().SetSource(new testUpdateNonePk()).ExecuteAffrows()); + g.sqlite.Insert(new testInsertNullable()).NoneParameter().ExecuteAffrows(); var ddlsql = g.sqlite.CodeFirst.GetComparisonDDLStatements(typeof(testInsertNullable), "tb123123"); Assert.Equal(@"CREATE TABLE IF NOT EXISTS ""main"".""tb123123"" ( diff --git a/FreeSql/FreeSql.xml b/FreeSql/FreeSql.xml index 4f5d13e2..b4918bb3 100644 --- a/FreeSql/FreeSql.xml +++ b/FreeSql/FreeSql.xml @@ -2213,14 +2213,16 @@ - 更新数据,设置更新的实体 + 更新数据,设置更新的实体 + 注意:实体必须定义主键,并且最终会自动附加条件 where id = source.Id 实体 - 更新数据,设置更新的实体集合 + 更新数据,设置更新的实体集合 + 注意:实体必须定义主键,并且最终会自动附加条件 where id in (source.Id) 实体集合 @@ -2592,145 +2594,6 @@ - - - 查询,若使用读写分离,查询【从库】条件cmdText.StartsWith("SELECT "),否则查询【主库】 - - - - - - - - - 查询,ExecuteReaderAsync(dr => {}, "select * from user where age > ?age", new { age = 25 }) - 提示:parms 参数还可以传 Dictionary<string, object> - - - - - - - 查询 - - - - - - - 查询,ExecuteArrayAsync("select * from user where age > ?age", new { age = 25 }) - 提示:parms 参数还可以传 Dictionary<string, object> - - - - - - - - 查询 - - - - - - - 查询,ExecuteDataSetAsync("select * from user where age > ?age; select 2", new { age = 25 }) - 提示:parms 参数还可以传 Dictionary<string, object> - - - - - - - - 查询 - - - - - - - 查询,ExecuteDataTableAsync("select * from user where age > ?age", new { age = 25 }) - 提示:parms 参数还可以传 Dictionary<string, object> - - - - - - - - 在【主库】执行 - - - - - - - - 在【主库】执行,ExecuteNonQueryAsync("delete from user where age > ?age", new { age = 25 }) - 提示:parms 参数还可以传 Dictionary<string, object> - - - - - - - - 在【主库】执行 - - - - - - - - 在【主库】执行,ExecuteScalarAsync("select 1 from user where age > ?age", new { age = 25 }) - 提示:parms 参数还可以传 Dictionary<string, object> - - - - - - - - 执行SQL返回对象集合,QueryAsync<User>("select * from user where age > ?age", new SqlParameter { ParameterName = "age", Value = 25 }) - - - - - - - - - - 执行SQL返回对象集合,QueryAsync<User>("select * from user where age > ?age", new { age = 25 }) - 提示:parms 参数还可以传 Dictionary<string, object> - - - - - - - - - 执行SQL返回对象集合,Query<User>("select * from user where age > ?age; select * from address", new SqlParameter { ParameterName = "age", Value = 25 }) - - - - - - - - - - 执行SQL返回对象集合,Query<User, Address>("select * from user where age > ?age; select * from address", new { age = 25 }) - 提示:parms 参数还可以传 Dictionary<string, object> - - - - - - 可自定义解析表达式 @@ -3416,12 +3279,6 @@ 超时 - - - 获取资源 - - - 使用完毕后,归还资源 @@ -3492,12 +3349,6 @@ 资源对象 - - - 从对象池获取对象成功的时候触发,通过该方法统计或初始化对象 - - 资源对象 - 归还对象给对象池的时候触发 diff --git a/FreeSql/Interface/Curd/IUpdate.cs b/FreeSql/Interface/Curd/IUpdate.cs index 5d4fb144..2abbdf64 100644 --- a/FreeSql/Interface/Curd/IUpdate.cs +++ b/FreeSql/Interface/Curd/IUpdate.cs @@ -54,13 +54,15 @@ namespace FreeSql IUpdate BatchProgress(Action> callback); /// - /// 更新数据,设置更新的实体 + /// 更新数据,设置更新的实体 + /// 注意:实体必须定义主键,并且最终会自动附加条件 where id = source.Id /// /// 实体 /// IUpdate SetSource(T1 source); /// - /// 更新数据,设置更新的实体集合 + /// 更新数据,设置更新的实体集合 + /// 注意:实体必须定义主键,并且最终会自动附加条件 where id in (source.Id) /// /// 实体集合 /// diff --git a/FreeSql/Internal/CommonProvider/UpdateProvider.cs b/FreeSql/Internal/CommonProvider/UpdateProvider.cs index 1c87af5b..72a31b3b 100644 --- a/FreeSql/Internal/CommonProvider/UpdateProvider.cs +++ b/FreeSql/Internal/CommonProvider/UpdateProvider.cs @@ -750,7 +750,10 @@ namespace FreeSql.Internal.CommonProvider sb.Append(" \r\nWHERE "); if (_source.Any()) + { + if (_table.Primarys.Any() == false) throw new ArgumentException($"{_table.Type.DisplayCsharp()} 没有定义主键,无法使用 SetSource,请尝试 SetDto"); sb.Append("(").Append(_commonUtils.WhereItems(_table, "", _source)).Append(")"); + } if (_where.Length > 0) sb.Append(_source.Any() ? _where.ToString() : _where.ToString().Substring(5));