Skip to content

Commit 8d92ccd

Browse files
2881028810
authored andcommitted
## 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;
1 parent 52450dc commit 8d92ccd

File tree

24 files changed

+92
-24
lines changed

24 files changed

+92
-24
lines changed

Extensions/FreeSql.Extensions.BaseEntity/FreeSql.Extensions.BaseEntity.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFrameworks>netstandard2.0</TargetFrameworks>
5-
<Version>0.9.15</Version>
5+
<Version>0.9.16</Version>
66
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
77
<Authors>YeXiangQin</Authors>
88
<Description>BaseEntity 是一种极简单的 CodeFirst 开发方式,特别对单表或多表CRUD,利用继承节省了每个实体类的重复属性(创建时间、ID等字段),软件删除等功能,进行 crud 操作时不必时常考虑仓储的使用.</Description>

Extensions/FreeSql.Extensions.JsonMap/FreeSql.Extensions.JsonMap.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFrameworks>netstandard2.0;net45</TargetFrameworks>
5-
<Version>0.9.15</Version>
5+
<Version>0.9.16</Version>
66
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
77
<Authors>YeXiangQin</Authors>
88
<Description>FreeSql 扩展包,可实现实体类属性为对象时,以JSON形式映射存储.</Description>

Extensions/FreeSql.Extensions.LazyLoading/FreeSql.Extensions.LazyLoading.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFrameworks>netstandard2.0;net45</TargetFrameworks>
5-
<Version>0.9.15</Version>
5+
<Version>0.9.16</Version>
66
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
77
<Authors>YeXiangQin</Authors>
88
<Description>FreeSql 扩展包,可实现【延时加载】属性.</Description>

FreeSql.DbContext/DbContext/DbContext.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,17 @@ public virtual IDbSet Set(Type entityType)
129129
/// <param name="data"></param>
130130
public void Attach<TEntity>(TEntity data) where TEntity : class => this.Set<TEntity>().Attach(data);
131131
public void AttachRange<TEntity>(IEnumerable<TEntity> data) where TEntity : class => this.Set<TEntity>().AttachRange(data);
132+
133+
/// <summary>
134+
/// 附加实体,并且只附加主键值,可用于不更新属性值为null或默认值的字段
135+
/// </summary>
136+
/// <typeparam name="TEntity"></typeparam>
137+
/// <param name="data"></param>
138+
public DbContext AttachOnlyPrimary<TEntity>(TEntity data) where TEntity : class
139+
{
140+
this.Set<TEntity>().AttachOnlyPrimary(data);
141+
return this;
142+
}
132143
#endregion
133144

134145
#region Queue Action

FreeSql.DbContext/DbSet/DbSet.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,23 @@ public void AttachRange(IEnumerable<TEntity> data)
162162
});
163163
}
164164
}
165+
/// <summary>
166+
/// 附加实体,并且只附加主键值,可用于不更新属性值为null或默认值的字段
167+
/// </summary>
168+
/// <param name="data"></param>
169+
public DbSet<TEntity> AttachOnlyPrimary(TEntity data)
170+
{
171+
if (data == null) return this;
172+
var pkitem = (TEntity)Activator.CreateInstance(_entityType);
173+
foreach (var pk in _db.Orm.CodeFirst.GetTableByEntity(_entityType).Primarys)
174+
{
175+
var colVal = _db.Orm.GetEntityValueWithPropertyName(_entityType, data, pk.CsName);
176+
_db.Orm.SetEntityValueWithPropertyName(_entityType, pkitem, pk.CsName, colVal);
177+
}
178+
this.Attach(pkitem);
179+
return this;
180+
}
181+
165182
/// <summary>
166183
/// 清空状态数据
167184
/// </summary>

FreeSql.DbContext/FreeSql.DbContext.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFrameworks>netstandard2.0;net45</TargetFrameworks>
5-
<Version>0.9.15</Version>
5+
<Version>0.9.16</Version>
66
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
77
<Authors>YeXiangQin</Authors>
88
<Description>FreeSql is the most convenient ORM in dotnet. It supports Mysql, Postgresql, SqlServer, Oracle and Sqlite.</Description>

FreeSql.DbContext/FreeSql.DbContext.xml

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

FreeSql.DbContext/Repository/Repository/BaseRepository.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,11 @@ public Task<int> UpdateAsync(IEnumerable<TEntity> entitys)
140140

141141
public void Attach(TEntity data) => _db.Attach(data);
142142
public void Attach(IEnumerable<TEntity> data) => _db.AttachRange(data);
143+
public IBasicRepository<TEntity> AttachOnlyPrimary(TEntity data)
144+
{
145+
_db.AttachOnlyPrimary(data);
146+
return this;
147+
}
143148
public void FlushState() => _dbset.FlushState();
144149

145150
public TEntity InsertOrUpdate(TEntity entity)

FreeSql.DbContext/Repository/Repository/IBasicRepository.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ public interface IBasicRepository<TEntity> : IReadOnlyRepository<TEntity>
2121
/// <param name="entity"></param>
2222
void Attach(TEntity entity);
2323
void Attach(IEnumerable<TEntity> entity);
24+
/// <summary>
25+
/// 附加实体,并且只附加主键值,可用于不更新属性值为null或默认值的字段
26+
/// </summary>
27+
/// <param name="data"></param>
28+
IBasicRepository<TEntity> AttachOnlyPrimary(TEntity data);
29+
2430
int Update(TEntity entity);
2531
int Update(IEnumerable<TEntity> entitys);
2632
Task<int> UpdateAsync(TEntity entity);

FreeSql.Repository/FreeSql.Repository.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFrameworks>netstandard2.0;net45</TargetFrameworks>
5-
<Version>0.9.15</Version>
5+
<Version>0.9.16</Version>
66
<Authors>YeXiangQin</Authors>
77
<Description>FreeSql Implementation of General Repository, Support MySql/SqlServer/PostgreSQL/Oracle/Sqlite, and read/write separation、and split table.</Description>
88
<PackageProjectUrl>https://github.com/2881099/FreeSql/wiki/Repository</PackageProjectUrl>

0 commit comments

Comments
 (0)