Skip to content

Commit 9152238

Browse files
committed
- 优化 DbContext/Repository Update 实体有 ServerTime 既使无状态变化也必然更新的逻辑;
1 parent 46ad51c commit 9152238

File tree

5 files changed

+60
-10
lines changed

5 files changed

+60
-10
lines changed

FreeSql.DbContext/FreeSql.DbContext.xml

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

FreeSql.Tests/FreeSql.Tests.DbContext/RepositoryTests.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,55 @@ class BeginEdit01
593593
public Guid Id { get; set; }
594594
public string Name { get; set; }
595595
}
596+
[Fact]
597+
public void BeginEditIdentity()
598+
{
599+
g.sqlite.Delete<BeginEdit02>().Where("1=1").ExecuteAffrows();
600+
var repo = g.sqlite.GetRepository<BeginEdit02>();
601+
var cts = new[] {
602+
new BeginEdit02 { Name = "分类1" },
603+
new BeginEdit02 { Name = "分类1_1" },
604+
new BeginEdit02 { Name = "分类1_2" },
605+
new BeginEdit02 { Name = "分类1_3" },
606+
new BeginEdit02 { Name = "分类2" },
607+
new BeginEdit02 { Name = "分类2_1" },
608+
new BeginEdit02 { Name = "分类2_2" }
609+
}.ToList();
610+
repo.Insert(cts);
611+
612+
repo.BeginEdit(cts);
613+
614+
cts.Add(new BeginEdit02 { Name = "分类2_3" });
615+
cts[0].Name = "123123";
616+
cts.RemoveAt(1);
617+
618+
Assert.Equal(3, repo.EndEdit());
619+
620+
g.sqlite.Delete<BeginEdit02>().Where("1=1").ExecuteAffrows();
621+
repo = g.sqlite.GetRepository<BeginEdit02>();
622+
cts = repo.Select.ToList();
623+
repo.BeginEdit(cts);
624+
625+
cts.AddRange(new[] {
626+
new BeginEdit02 { Name = "分类1" },
627+
new BeginEdit02 { Name = "分类1_1" },
628+
new BeginEdit02 { Name = "分类1_2" },
629+
new BeginEdit02 { Name = "分类1_3" },
630+
new BeginEdit02 { Name = "分类2" },
631+
new BeginEdit02 { Name = "分类2_1" },
632+
new BeginEdit02 { Name = "分类2_2" }
633+
});
634+
635+
Assert.Equal(7, repo.EndEdit());
636+
}
637+
class BeginEdit02
638+
{
639+
[Column(IsIdentity = true)]
640+
public int Id { get; set; }
641+
public string Name { get; set; }
642+
[Column(ServerTime = DateTimeKind.Utc)]
643+
public DateTime UpdateTime { get; set; }
644+
}
596645

597646
[Fact]
598647
public void OrmScoped()

FreeSql/Extensions/EntityUtilExtensions.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,15 @@ public static string[] CompareEntityValueReturnColumns(this IFreeSql orm, Type e
604604
exps.Add(Expression.Label(returnTarget, Expression.Constant(new string[0])));
605605
return Expression.Lambda<Func<object, object, bool, string[]>>(Expression.Block(new[] { var1Ret, var1Parm, var2Parm }, exps), new[] { parm1, parm2, parm3 }).Compile();
606606
});
607-
return func(entity1, entity2, isEqual);
607+
var result = func(entity1, entity2, isEqual);
608+
var tmptb = orm.CodeFirst.GetTableByEntity(entityType);
609+
if (tmptb.ColumnsByCanUpdateDbUpdateValue.Length > 0) {
610+
if (isEqual && result.Length + tmptb.ColumnsByCanUpdateDbUpdateValue.Length == tmptb.ColumnsByCs.Count)
611+
return result.Concat(tmptb.ColumnsByCanUpdateDbUpdateValue.Select(a => a.Attribute.Name)).ToArray();
612+
if (!isEqual && result.Length == tmptb.ColumnsByCanUpdateDbUpdateValue.Length)
613+
return new string[0];
614+
}
615+
return result;
608616
}
609617

610618
static ConcurrentDictionary<DataType, ConcurrentDictionary<Type, Action<object, string, int>>> _dicSetEntityIncrByWithPropertyName = new ConcurrentDictionary<DataType, ConcurrentDictionary<Type, Action<object, string, int>>>();

FreeSql/Internal/Model/TableInfo.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public class TableInfo
1717
public Dictionary<string, ColumnInfo> ColumnsByCs { get; set; } = new Dictionary<string, ColumnInfo>(StringComparer.CurrentCultureIgnoreCase);
1818
public Dictionary<string, ColumnInfo> ColumnsByCsIgnore { get; set; } = new Dictionary<string, ColumnInfo>(StringComparer.CurrentCultureIgnoreCase);
1919
public ColumnInfo[] ColumnsByPosition { get; set; }
20+
public ColumnInfo[] ColumnsByCanUpdateDbUpdateValue { get; set; }
2021
public ColumnInfo[] Primarys { get; set; }
2122
public IndexInfo[] Indexes { get; set; }
2223
public string CsName { get; set; }

FreeSql/Internal/UtilsExpressionTree.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,7 @@ internal static TableInfo GetTableByEntity(Type entity, CommonUtils common)
484484
trytb.ColumnsByPosition = columnsList.Where(a => a.Attribute.Position > 0).OrderBy(a => a.Attribute.Position)
485485
.Concat(columnsList.Where(a => a.Attribute.Position == 0))
486486
.Concat(columnsList.Where(a => a.Attribute.Position < 0).OrderBy(a => a.Attribute.Position)).ToArray();
487+
trytb.ColumnsByCanUpdateDbUpdateValue = columnsList.Where(a => a.Attribute.CanUpdate == true && string.IsNullOrEmpty(a.DbUpdateValue) == false).ToArray();
487488

488489
trytb.Primarys = trytb.Columns.Values.Where(a => a.Attribute.IsPrimary == true).ToArray();
489490
if (trytb.Primarys.Any() == false)

0 commit comments

Comments
 (0)