dotnetcore / FreeSql

🦄 .NET aot orm, C# orm, VB.NET orm, Mysql orm, Postgresql orm, SqlServer orm, Oracle orm, Sqlite orm, Firebird orm, 达梦 orm, 人大金仓 orm, 神通 orm, 翰高 orm, 南大通用 orm, 虚谷 orm, 国产 orm, Clickhouse orm, QuestDB orm, MsAccess orm.
http://freesql.net
MIT License
4k stars 839 forks source link

在工作单元中使用Update时是否可以在后面加where条件 #166

Closed landonzeng closed 4 years ago

landonzeng commented 4 years ago

_uow.GetGuidRepository

().UpdateAsync(model) 在项目中使用工作单元的时候发现Update/UpdateAsync()方法前后都不能加Where(),是我用法不对么?

2881099 commented 4 years ago

对,仓储只能当实体主键值作条件。

已经传入了实体,还需要其他条件吗?

landonzeng commented 4 years ago

如果要加条件更新的话,只能使用Select ToUpdate() set的方式吧?

landonzeng commented 4 years ago

有时候需要批量更新有点不方便

2881099 commented 4 years ago
var repo = _uow.GetGuidRepository<T>();
rep.UpdateDiy.SetSource(model).Where(a => ....).ExecuteAffrows();

ISelect.ToUpdate 建议在数据整理的场景下使用。

landonzeng commented 4 years ago

好吧,那如果需要根据条件批量更新的话,怎么处理比较好呢?

2881099 commented 4 years ago

批量更新有两种情况:

1、已经有实体列表了,使用 Repository.Update() 或者 IUpdate.SetSource() 都可以

2、没有实体列表,IUpdate.Where(a => a.socre > 100).Set(a => score + 5).ExecuteAffrows(),这种产生的SQL:

update t set score = score + 5 where score > 100
landonzeng commented 4 years ago

ok

landonzeng commented 4 years ago

IRepositoryUnitOfWork 下就只能用 _uow.GetRepository

().UpdateDiy.SetSource(model).Where(it=>it.F_EnCode=="123"); 这种类型了

2881099 commented 4 years ago

UpdateDiy

UpdateDiy 的效果,和 fsql.Update\<T>() 效果一样

landonzeng commented 4 years ago

好的

landonzeng commented 4 years ago

发现个问题,sql里面默认加了主键条件 QQ截图20191223182211

2881099 commented 4 years ago

SetSource 是会按主键增加条件

landonzeng commented 4 years ago

要用SetRow吗?还能用其他的方法吗?

2881099 commented 4 years ago

SetSource 是更新整个实体,会自动增加主键值为 where 条件

SetRaw 是传 sql 文本进去更新,其实是 update set ... 这里的内容,需要手动设置条件

Set 是指定一个字段更新,Set(a => a.Code == "xxx",需要手动设置条件

landonzeng commented 4 years ago

好的,总算搞明白了,