mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-06-18 20:08:15 +08:00
## v0.9.16
- 增加 BaseRepository.AttachOnlyPrimary 方法,只附加实体的主键值; > 在更新前使用可实现不查询数据库再更新、也可以实现更新时不更新值为 null 的字段 ```csharp class T { public int id { get; set; } public string name { get; set; } public string other { get; set; } } var item = new T { id = 1, name = "xx" }; fsql.GetRepository<T>().AttachOnlyPrimary(item).Update(item); //只更新 name ``` - 修复 Lambda 表达式中 DateTime.Now.ToString("yyyyMMdd") 不能直接执行的 bug;
This commit is contained in:
@ -129,6 +129,17 @@ namespace FreeSql
|
||||
/// <param name="data"></param>
|
||||
public void Attach<TEntity>(TEntity data) where TEntity : class => this.Set<TEntity>().Attach(data);
|
||||
public void AttachRange<TEntity>(IEnumerable<TEntity> data) where TEntity : class => this.Set<TEntity>().AttachRange(data);
|
||||
|
||||
/// <summary>
|
||||
/// 附加实体,并且只附加主键值,可用于不更新属性值为null或默认值的字段
|
||||
/// </summary>
|
||||
/// <typeparam name="TEntity"></typeparam>
|
||||
/// <param name="data"></param>
|
||||
public DbContext AttachOnlyPrimary<TEntity>(TEntity data) where TEntity : class
|
||||
{
|
||||
this.Set<TEntity>().AttachOnlyPrimary(data);
|
||||
return this;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Queue Action
|
||||
|
@ -162,6 +162,23 @@ namespace FreeSql
|
||||
});
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 附加实体,并且只附加主键值,可用于不更新属性值为null或默认值的字段
|
||||
/// </summary>
|
||||
/// <param name="data"></param>
|
||||
public DbSet<TEntity> AttachOnlyPrimary(TEntity data)
|
||||
{
|
||||
if (data == null) return this;
|
||||
var pkitem = (TEntity)Activator.CreateInstance(_entityType);
|
||||
foreach (var pk in _db.Orm.CodeFirst.GetTableByEntity(_entityType).Primarys)
|
||||
{
|
||||
var colVal = _db.Orm.GetEntityValueWithPropertyName(_entityType, data, pk.CsName);
|
||||
_db.Orm.SetEntityValueWithPropertyName(_entityType, pkitem, pk.CsName, colVal);
|
||||
}
|
||||
this.Attach(pkitem);
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 清空状态数据
|
||||
/// </summary>
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFrameworks>netstandard2.0;net45</TargetFrameworks>
|
||||
<Version>0.9.15</Version>
|
||||
<Version>0.9.16</Version>
|
||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||
<Authors>YeXiangQin</Authors>
|
||||
<Description>FreeSql is the most convenient ORM in dotnet. It supports Mysql, Postgresql, SqlServer, Oracle and Sqlite.</Description>
|
||||
|
@ -39,6 +39,13 @@
|
||||
<typeparam name="TEntity"></typeparam>
|
||||
<param name="data"></param>
|
||||
</member>
|
||||
<member name="M:FreeSql.DbContext.AttachOnlyPrimary``1(``0)">
|
||||
<summary>
|
||||
附加实体,并且只附加主键值,可用于不更新属性值为null或默认值的字段
|
||||
</summary>
|
||||
<typeparam name="TEntity"></typeparam>
|
||||
<param name="data"></param>
|
||||
</member>
|
||||
<member name="P:FreeSql.DbContextOptions.EnableAddOrUpdateNavigateList">
|
||||
<summary>
|
||||
是否开启一对多,联级保存功能
|
||||
@ -57,6 +64,12 @@
|
||||
</summary>
|
||||
<param name="data"></param>
|
||||
</member>
|
||||
<member name="M:FreeSql.DbSet`1.AttachOnlyPrimary(`0)">
|
||||
<summary>
|
||||
附加实体,并且只附加主键值,可用于不更新属性值为null或默认值的字段
|
||||
</summary>
|
||||
<param name="data"></param>
|
||||
</member>
|
||||
<member name="M:FreeSql.DbSet`1.FlushState">
|
||||
<summary>
|
||||
清空状态数据
|
||||
@ -156,6 +169,12 @@
|
||||
</summary>
|
||||
<param name="entity"></param>
|
||||
</member>
|
||||
<member name="M:FreeSql.IBasicRepository`1.AttachOnlyPrimary(`0)">
|
||||
<summary>
|
||||
附加实体,并且只附加主键值,可用于不更新属性值为null或默认值的字段
|
||||
</summary>
|
||||
<param name="data"></param>
|
||||
</member>
|
||||
<member name="P:FreeSql.IUnitOfWork.Enable">
|
||||
<summary>
|
||||
是否启用工作单元
|
||||
|
@ -140,6 +140,11 @@ namespace FreeSql
|
||||
|
||||
public void Attach(TEntity data) => _db.Attach(data);
|
||||
public void Attach(IEnumerable<TEntity> data) => _db.AttachRange(data);
|
||||
public IBasicRepository<TEntity> AttachOnlyPrimary(TEntity data)
|
||||
{
|
||||
_db.AttachOnlyPrimary(data);
|
||||
return this;
|
||||
}
|
||||
public void FlushState() => _dbset.FlushState();
|
||||
|
||||
public TEntity InsertOrUpdate(TEntity entity)
|
||||
|
@ -21,6 +21,12 @@ namespace FreeSql
|
||||
/// <param name="entity"></param>
|
||||
void Attach(TEntity entity);
|
||||
void Attach(IEnumerable<TEntity> entity);
|
||||
/// <summary>
|
||||
/// 附加实体,并且只附加主键值,可用于不更新属性值为null或默认值的字段
|
||||
/// </summary>
|
||||
/// <param name="data"></param>
|
||||
IBasicRepository<TEntity> AttachOnlyPrimary(TEntity data);
|
||||
|
||||
int Update(TEntity entity);
|
||||
int Update(IEnumerable<TEntity> entitys);
|
||||
Task<int> UpdateAsync(TEntity entity);
|
||||
|
Reference in New Issue
Block a user