Skip to content

Commit

Permalink
- 优化 DbContext/Repository Update 实体有 ServerTime 既使无状态变化也必然更新的逻辑;
Browse files Browse the repository at this point in the history
  • Loading branch information
2881099 committed Apr 12, 2022
1 parent 46ad51c commit 9152238
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 10 deletions.
9 changes: 0 additions & 9 deletions FreeSql.DbContext/FreeSql.DbContext.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 49 additions & 0 deletions FreeSql.Tests/FreeSql.Tests.DbContext/RepositoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,55 @@ class BeginEdit01
public Guid Id { get; set; }
public string Name { get; set; }
}
[Fact]
public void BeginEditIdentity()
{
g.sqlite.Delete<BeginEdit02>().Where("1=1").ExecuteAffrows();
var repo = g.sqlite.GetRepository<BeginEdit02>();
var cts = new[] {
new BeginEdit02 { Name = "分类1" },
new BeginEdit02 { Name = "分类1_1" },
new BeginEdit02 { Name = "分类1_2" },
new BeginEdit02 { Name = "分类1_3" },
new BeginEdit02 { Name = "分类2" },
new BeginEdit02 { Name = "分类2_1" },
new BeginEdit02 { Name = "分类2_2" }
}.ToList();
repo.Insert(cts);

repo.BeginEdit(cts);

cts.Add(new BeginEdit02 { Name = "分类2_3" });
cts[0].Name = "123123";
cts.RemoveAt(1);

Assert.Equal(3, repo.EndEdit());

g.sqlite.Delete<BeginEdit02>().Where("1=1").ExecuteAffrows();
repo = g.sqlite.GetRepository<BeginEdit02>();
cts = repo.Select.ToList();
repo.BeginEdit(cts);

cts.AddRange(new[] {
new BeginEdit02 { Name = "分类1" },
new BeginEdit02 { Name = "分类1_1" },
new BeginEdit02 { Name = "分类1_2" },
new BeginEdit02 { Name = "分类1_3" },
new BeginEdit02 { Name = "分类2" },
new BeginEdit02 { Name = "分类2_1" },
new BeginEdit02 { Name = "分类2_2" }
});

Assert.Equal(7, repo.EndEdit());
}
class BeginEdit02
{
[Column(IsIdentity = true)]
public int Id { get; set; }
public string Name { get; set; }
[Column(ServerTime = DateTimeKind.Utc)]
public DateTime UpdateTime { get; set; }
}

[Fact]
public void OrmScoped()
Expand Down
10 changes: 9 additions & 1 deletion FreeSql/Extensions/EntityUtilExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,15 @@ public static string[] CompareEntityValueReturnColumns(this IFreeSql orm, Type e
exps.Add(Expression.Label(returnTarget, Expression.Constant(new string[0])));
return Expression.Lambda<Func<object, object, bool, string[]>>(Expression.Block(new[] { var1Ret, var1Parm, var2Parm }, exps), new[] { parm1, parm2, parm3 }).Compile();
});
return func(entity1, entity2, isEqual);
var result = func(entity1, entity2, isEqual);
var tmptb = orm.CodeFirst.GetTableByEntity(entityType);
if (tmptb.ColumnsByCanUpdateDbUpdateValue.Length > 0) {
if (isEqual && result.Length + tmptb.ColumnsByCanUpdateDbUpdateValue.Length == tmptb.ColumnsByCs.Count)
return result.Concat(tmptb.ColumnsByCanUpdateDbUpdateValue.Select(a => a.Attribute.Name)).ToArray();
if (!isEqual && result.Length == tmptb.ColumnsByCanUpdateDbUpdateValue.Length)
return new string[0];
}
return result;
}

static ConcurrentDictionary<DataType, ConcurrentDictionary<Type, Action<object, string, int>>> _dicSetEntityIncrByWithPropertyName = new ConcurrentDictionary<DataType, ConcurrentDictionary<Type, Action<object, string, int>>>();
Expand Down
1 change: 1 addition & 0 deletions FreeSql/Internal/Model/TableInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class TableInfo
public Dictionary<string, ColumnInfo> ColumnsByCs { get; set; } = new Dictionary<string, ColumnInfo>(StringComparer.CurrentCultureIgnoreCase);
public Dictionary<string, ColumnInfo> ColumnsByCsIgnore { get; set; } = new Dictionary<string, ColumnInfo>(StringComparer.CurrentCultureIgnoreCase);
public ColumnInfo[] ColumnsByPosition { get; set; }
public ColumnInfo[] ColumnsByCanUpdateDbUpdateValue { get; set; }
public ColumnInfo[] Primarys { get; set; }
public IndexInfo[] Indexes { get; set; }
public string CsName { get; set; }
Expand Down
1 change: 1 addition & 0 deletions FreeSql/Internal/UtilsExpressionTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,7 @@ internal static TableInfo GetTableByEntity(Type entity, CommonUtils common)
trytb.ColumnsByPosition = columnsList.Where(a => a.Attribute.Position > 0).OrderBy(a => a.Attribute.Position)
.Concat(columnsList.Where(a => a.Attribute.Position == 0))
.Concat(columnsList.Where(a => a.Attribute.Position < 0).OrderBy(a => a.Attribute.Position)).ToArray();
trytb.ColumnsByCanUpdateDbUpdateValue = columnsList.Where(a => a.Attribute.CanUpdate == true && string.IsNullOrEmpty(a.DbUpdateValue) == false).ToArray();

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

0 comments on commit 9152238

Please sign in to comment.