Skip to content

Commit

Permalink
Merge pull request #139 from netcorepal/fix-rowversion-set-method-not…
Browse files Browse the repository at this point in the history
…-found

fix: error Property set method not found when using RowVersion and Us…
  • Loading branch information
witskeeper authored Jan 9, 2025
2 parents 95b344d + 22434e1 commit 45778d7
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 7 deletions.
1 change: 1 addition & 0 deletions Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
<PackageReference Update="Microsoft.SourceLink.GitHub" Version="8.0.0" />
<!--microsoft entity framework -->
<PackageReference Update="Microsoft.EntityFrameworkCore" Version="$(EntityFrameworkVersion)"/>
<PackageReference Update="Microsoft.EntityFrameworkCore.Proxies" Version="$(EntityFrameworkVersion)" />
<PackageReference Update="Microsoft.EntityFrameworkCore.Relational" Version="$(EntityFrameworkVersion)"/>
<PackageReference Update="Microsoft.EntityFrameworkCore.Sqlite" Version="$(EntityFrameworkVersion)"/>
<PackageReference Update="Microsoft.EntityFrameworkCore.InMemory" Version="$(EntityFrameworkVersion)"/>
Expand Down
15 changes: 8 additions & 7 deletions src/Repository.EntityFrameworkCore/AppDbContextBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions test/NetCorePal.Web/NetCorePal.Web.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.9.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.9.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.9.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
Expand Down Expand Up @@ -68,4 +69,8 @@
<Folder Include="logs\" />
</ItemGroup>

<ItemGroup>
<Compile Remove="Jwt\JwtProvider.cs" />
</ItemGroup>

</Project>
1 change: 1 addition & 0 deletions test/NetCorePal.Web/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@
builder.Services.AddRepositories(typeof(ApplicationDbContext).Assembly);
builder.Services.AddDbContext<ApplicationDbContext>(options =>
{
options.UseLazyLoadingProxies();
#if NET10_0
options.UseNpgsql(builder.Configuration.GetConnectionString("PostgreSql"));
#else
Expand Down

0 comments on commit 45778d7

Please sign in to comment.