diff --git a/Directory.Build.targets b/Directory.Build.targets index c3288a7b..34a63b05 100644 --- a/Directory.Build.targets +++ b/Directory.Build.targets @@ -45,6 +45,7 @@ + diff --git a/src/Repository.EntityFrameworkCore/AppDbContextBase.cs b/src/Repository.EntityFrameworkCore/AppDbContextBase.cs index 6333d36e..f1d811a4 100644 --- a/src/Repository.EntityFrameworkCore/AppDbContextBase.cs +++ b/src/Repository.EntityFrameworkCore/AppDbContextBase.cs @@ -147,14 +147,15 @@ protected virtual void UpdateRowVersionBeforeSaveChanges(ChangeTracker changeTra { if (entry.State == EntityState.Modified) { - var entityType = entry.Entity.GetType(); - var properties = entityType.GetProperties(BindingFlags.Public | BindingFlags.Instance) - .Where(p => p.PropertyType == typeof(RowVersion)); - foreach (var property in properties) + foreach (var p in entry.Properties) { - var rowVersion = (RowVersion)property.GetValue(entry.Entity)!; - var newRowVersion = new RowVersion(VersionNumber: rowVersion.VersionNumber + 1); - property.SetValue(entry.Entity, newRowVersion); + if (p.Metadata.ClrType == typeof(RowVersion) && !p.IsModified) + { + var newValue = p.OriginalValue == null + ? new RowVersion(0) + : new RowVersion(((RowVersion)p.OriginalValue).VersionNumber + 1); + p.CurrentValue = newValue; + } } } } diff --git a/test/NetCorePal.Web/NetCorePal.Web.csproj b/test/NetCorePal.Web/NetCorePal.Web.csproj index f07424e4..750dc39c 100644 --- a/test/NetCorePal.Web/NetCorePal.Web.csproj +++ b/test/NetCorePal.Web/NetCorePal.Web.csproj @@ -28,6 +28,7 @@ + @@ -68,4 +69,8 @@ + + + + \ No newline at end of file diff --git a/test/NetCorePal.Web/Program.cs b/test/NetCorePal.Web/Program.cs index 9307e6de..ffb50d3a 100644 --- a/test/NetCorePal.Web/Program.cs +++ b/test/NetCorePal.Web/Program.cs @@ -153,6 +153,7 @@ builder.Services.AddRepositories(typeof(ApplicationDbContext).Assembly); builder.Services.AddDbContext(options => { + options.UseLazyLoadingProxies(); #if NET10_0 options.UseNpgsql(builder.Configuration.GetConnectionString("PostgreSql")); #else