添加部分更新,部分不更新

This commit is contained in:
xuejiaming 2021-04-12 17:20:13 +08:00
parent b73127b11a
commit 352e2a07fa
5 changed files with 87 additions and 3 deletions

View File

@ -1,8 +1,8 @@
:start
::定义版本
set EFCORE2=2.1.0.10
set EFCORE3=3.1.0.10
set EFCORE5=5.1.0.10
set EFCORE2=2.1.0.11
set EFCORE3=3.1.0.11
set EFCORE5=5.1.0.11
::删除所有bin与obj下的文件
@echo off

View File

@ -29,6 +29,8 @@ namespace ShardingCore.DbContexts.VirtualDbContexts
int Insert<T>(T entity) where T : class;
int InsertRange<T>(ICollection<T> entities) where T : class;
int Update<T>(T entity) where T : class;
void UpdateColumns<T>(T entity,Expression<Func<T,object>> getUpdatePropertyNames) where T : class;
void UpdateWithOutIgnoreColumns<T>(T entity,Expression<Func<T,object>> getIgnorePropertyNames) where T : class;
int UpdateRange<T>(ICollection<T> entities) where T : class;
int Delete<T>(T entity) where T : class;
int DeleteRange<T>(ICollection<T> entities) where T : class;

View File

@ -208,6 +208,45 @@ namespace ShardingCore.DbContexts.VirtualDbContexts
return 1;
}
public void UpdateColumns<T>(T entity, Expression<Func<T, object>> getUpdatePropertyNames) where T : class
{
var context= CreateGenericDbContext(entity);
context.Set<T>().Attach(entity);
var props = GetUpdatePropNames(entity, getUpdatePropertyNames);
foreach (var prop in props)
{
context.Entry(entity).Property(prop).IsModified = true;
}
}
private IEnumerable<string> GetUpdatePropNames<T>(T entity, Expression<Func<T, object>> getUpdatePropertyNames) where T : class
{
var updatePropertyNames = getUpdatePropertyNames.Compile()(entity);
var fullPropNames = entity.GetType().GetProperties().Select(o => o.Name);
var updatePropNames = updatePropertyNames.GetType().GetProperties().Select(o => o.Name);
return updatePropNames.Intersect(fullPropNames);
}
public void UpdateWithOutIgnoreColumns<T>(T entity, Expression<Func<T, object>> getIgnorePropertyNames) where T : class
{
var context = CreateGenericDbContext(entity);
context.Entry(entity).State = EntityState.Modified;
var props = GetIgnorePropNames(entity, getIgnorePropertyNames);
foreach (var prop in props)
{
context.Entry(entity).Property(prop).IsModified = false;
}
}
private IEnumerable<string> GetIgnorePropNames<T>(T entity, Expression<Func<T, object>> getIgnorePropertyNames) where T : class
{
var ignoreProp = getIgnorePropertyNames.Compile()(entity);
var fullUpdatePropNames = entity.GetType().GetProperties().Select(o => o.Name);
var ignorePropNames = ignoreProp.GetType().GetProperties().Select(o => o.Name);
return ignorePropNames.Intersect(fullUpdatePropNames);
}
public int UpdateRange<T>(ICollection<T> entities) where T : class
{
var groups = entities.Select(o =>

View File

@ -18,4 +18,8 @@
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="5.0.2" />
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="5.0.0" />
</ItemGroup>
<ItemGroup>
<Folder Include="DbContexts\VirtualDbContexts\UpdatePart\" />
</ItemGroup>
</Project>

View File

@ -1,3 +1,4 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using ShardingCore.Core.VirtualRoutes.TableRoutes.RoutingRuleEngine;
@ -348,5 +349,43 @@ namespace ShardingCoreTestSqlServer3x
Assert.Equal(1120000, group[0].MinSalary);
Assert.Equal(1140000, group[0].MaxSalary);
}
[Fact]
public async Task UpdateColumn()
{
var item = await _virtualDbContext.Set<SysUserMod>().Where(o=>o.Id=="147").ShardingFirstOrDefaultAsync();
var oldName = item.Name;
var oldAge = item.Age;
var newName = $"test_{new Random().Next(0, 99999)}";
var newAge = new Random().Next(0, 99999);
item.Name = newName;
item.Age = newAge;
_virtualDbContext.UpdateColumns(item,o=>new {o.Name});
await _virtualDbContext.SaveChangesAsync();
var newItem = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Id == "147").ShardingFirstOrDefaultAsync();
Assert.Equal(newName, newItem.Name);
Assert.NotEqual(oldName, newItem.Name);
Assert.NotEqual(newAge, newItem.Age);
Assert.Equal(oldAge, newItem.Age);
}
[Fact]
public async Task UpdateIgnoreColumn()
{
var item = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Id == "153").ShardingFirstOrDefaultAsync();
var oldName = item.Name;
var oldAge = item.Age;
var newName = $"test_{new Random().Next(0, 99999)}";
var newAge = new Random().Next(0, 99999);
item.Name = newName;
item.Age = newAge;
_virtualDbContext.UpdateWithOutIgnoreColumns(item, o => new { o.Name });
await _virtualDbContext.SaveChangesAsync();
var newItem = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Id == "153").ShardingFirstOrDefaultAsync();
Assert.NotEqual(newName, newItem.Name);
Assert.Equal(oldName, newItem.Name);
Assert.Equal(newAge, newItem.Age);
Assert.NotEqual(oldAge, newItem.Age);
}
}
}