From 352e2a07fa3e9c195f804aa1026b463581e38fb7 Mon Sep 17 00:00:00 2001 From: xuejiaming <326308290@qq.com> Date: Mon, 12 Apr 2021 17:20:13 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=83=A8=E5=88=86=E6=9B=B4?= =?UTF-8?q?=E6=96=B0,=E9=83=A8=E5=88=86=E4=B8=8D=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nuget-publish.bat | 6 +-- .../VirtualDbContexts/IVirtualDbContext.cs | 2 + .../VirtualDbContexts/VirtualDbContext.cs | 39 +++++++++++++++++++ src/ShardingCore/ShardingCore.csproj | 4 ++ .../ShardingTest.cs | 39 +++++++++++++++++++ 5 files changed, 87 insertions(+), 3 deletions(-) diff --git a/nuget-publish.bat b/nuget-publish.bat index e45275e8..e226b0d7 100644 --- a/nuget-publish.bat +++ b/nuget-publish.bat @@ -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 diff --git a/src/ShardingCore/DbContexts/VirtualDbContexts/IVirtualDbContext.cs b/src/ShardingCore/DbContexts/VirtualDbContexts/IVirtualDbContext.cs index c80bd566..77afd91f 100644 --- a/src/ShardingCore/DbContexts/VirtualDbContexts/IVirtualDbContext.cs +++ b/src/ShardingCore/DbContexts/VirtualDbContexts/IVirtualDbContext.cs @@ -29,6 +29,8 @@ namespace ShardingCore.DbContexts.VirtualDbContexts int Insert(T entity) where T : class; int InsertRange(ICollection entities) where T : class; int Update(T entity) where T : class; + void UpdateColumns(T entity,Expression> getUpdatePropertyNames) where T : class; + void UpdateWithOutIgnoreColumns(T entity,Expression> getIgnorePropertyNames) where T : class; int UpdateRange(ICollection entities) where T : class; int Delete(T entity) where T : class; int DeleteRange(ICollection entities) where T : class; diff --git a/src/ShardingCore/DbContexts/VirtualDbContexts/VirtualDbContext.cs b/src/ShardingCore/DbContexts/VirtualDbContexts/VirtualDbContext.cs index 958b3971..9300a2e1 100644 --- a/src/ShardingCore/DbContexts/VirtualDbContexts/VirtualDbContext.cs +++ b/src/ShardingCore/DbContexts/VirtualDbContexts/VirtualDbContext.cs @@ -208,6 +208,45 @@ namespace ShardingCore.DbContexts.VirtualDbContexts return 1; } + + public void UpdateColumns(T entity, Expression> getUpdatePropertyNames) where T : class + { + var context= CreateGenericDbContext(entity); + context.Set().Attach(entity); + var props = GetUpdatePropNames(entity, getUpdatePropertyNames); + foreach (var prop in props) + { + context.Entry(entity).Property(prop).IsModified = true; + } + } + private IEnumerable GetUpdatePropNames(T entity, Expression> 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 entity, Expression> 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 GetIgnorePropNames(T entity, Expression> 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(ICollection entities) where T : class { var groups = entities.Select(o => diff --git a/src/ShardingCore/ShardingCore.csproj b/src/ShardingCore/ShardingCore.csproj index c2b032f3..ddf81cc3 100644 --- a/src/ShardingCore/ShardingCore.csproj +++ b/src/ShardingCore/ShardingCore.csproj @@ -18,4 +18,8 @@ + + + + diff --git a/test/ShardingCoreTestSqlServer3x/ShardingTest.cs b/test/ShardingCoreTestSqlServer3x/ShardingTest.cs index 17fea00d..7fe3644f 100644 --- a/test/ShardingCoreTestSqlServer3x/ShardingTest.cs +++ b/test/ShardingCoreTestSqlServer3x/ShardingTest.cs @@ -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().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().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().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().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); + } } } \ No newline at end of file