Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add application generated concurrency stamp to Sqlite in Boilerplate (#10162) #10176

Merged
merged 1 commit into from
Mar 5, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -13,6 +13,9 @@
//#if (notification == true)
using Boilerplate.Server.Api.Models.PushNotification;
//#endif
//#if (database == "Sqlite")
using System.Security.Cryptography;
//#endif

namespace Boilerplate.Server.Api.Data;

@@ -54,18 +57,14 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)

ConfigureIdentityTableNames(modelBuilder);

//#if (database != "Sqlite")
ConfigureConcurrencyStamp(modelBuilder);
//#endif
}

public override int SaveChanges(bool acceptAllChangesOnSuccess)
{
try
{
//#if (database != "Sqlite")
ReplaceOriginalConcurrencyStamp();
//#endif
SetConcurrencyStamp();

return base.SaveChanges(acceptAllChangesOnSuccess);
}
@@ -79,9 +78,7 @@ public override int SaveChanges(bool acceptAllChangesOnSuccess)
{
try
{
//#if (database != "Sqlite")
ReplaceOriginalConcurrencyStamp();
//#endif
SetConcurrencyStamp();

return await base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken);
}
@@ -91,16 +88,8 @@ public override int SaveChanges(bool acceptAllChangesOnSuccess)
}
}

//#if (database != "Sqlite")
/// <summary>
/// https://github.com/dotnet/efcore/issues/35443
/// </summary>
private void ReplaceOriginalConcurrencyStamp()
private void SetConcurrencyStamp()
{
//#if (IsInsideProjectTemplate == true)
if (Database.ProviderName!.EndsWith("Sqlite", StringComparison.InvariantCulture))
return;
//#endif
ChangeTracker.DetectChanges();

foreach (var entityEntry in ChangeTracker.Entries().Where(e => e.State is EntityState.Modified or EntityState.Deleted))
@@ -109,10 +98,28 @@ private void ReplaceOriginalConcurrencyStamp()
|| currentConcurrencyStamp is not byte[])
continue;

entityEntry.OriginalValues.SetValues(new Dictionary<string, object> { { "ConcurrencyStamp", currentConcurrencyStamp } });
//#if (database != "Sqlite")
//#if (IsInsideProjectTemplate == true)
if (Database.ProviderName!.EndsWith("Sqlite", StringComparison.InvariantCulture) is false)
{
//#endif
// https://github.com/dotnet/efcore/issues/35443
entityEntry.OriginalValues.SetValues(new Dictionary<string, object> { { "ConcurrencyStamp", currentConcurrencyStamp } });
//#if (IsInsideProjectTemplate == true)
}
//#endif
//#else
//#if (IsInsideProjectTemplate == true)
if (Database.ProviderName!.EndsWith("Sqlite", StringComparison.InvariantCulture))
{
//#endif
entityEntry.CurrentValues.SetValues(new Dictionary<string, object> { { "ConcurrencyStamp", RandomNumberGenerator.GetBytes(8) } });
//#if (IsInsideProjectTemplate == true)
}
//#endif
//#endif
}
}
//#endif

protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
{
@@ -180,25 +187,31 @@ private void ConfigureIdentityTableNames(ModelBuilder builder)
.ToTable("UserClaims");
}

//#if (database != "Sqlite")
private void ConfigureConcurrencyStamp(ModelBuilder modelBuilder)
{
//#if (IsInsideProjectTemplate == true)
if (Database.ProviderName!.EndsWith("Sqlite", StringComparison.InvariantCulture))
return;
//#endif

foreach (var entityType in modelBuilder.Model.GetEntityTypes())
{
foreach (var property in entityType.GetProperties()
.Where(p => p.Name is "ConcurrencyStamp" && p.PropertyInfo?.PropertyType == typeof(byte[])))
{
var builder = new PropertyBuilder(property);

//#if (database == "Sqlite")
//#if (IsInsideProjectTemplate == true)
if (Database.ProviderName!.EndsWith("Sqlite", StringComparison.InvariantCulture))
{
//#endif
builder.IsConcurrencyToken();
//#if (IsInsideProjectTemplate == true)
continue;
}
//#endif
//#else
builder.IsConcurrencyToken()
.IsRowVersion();

//#if (IsInsideProjectTemplate == true)
if (Database.ProviderName.EndsWith("PostgreSQL", StringComparison.InvariantCulture))
if (Database.ProviderName!.EndsWith("PostgreSQL", StringComparison.InvariantCulture))
{
//#endif
//#if (database == "PostgreSQL")
@@ -209,8 +222,8 @@ private void ConfigureConcurrencyStamp(ModelBuilder modelBuilder)
//#if (IsInsideProjectTemplate == true)
}
//#endif
//#endif
}
}
}
//#endif
}

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

Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
// <auto-generated />
using System;
using Boilerplate.Server.Api.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;

#nullable disable

@@ -23,6 +27,7 @@ protected override void BuildModel(ModelBuilder modelBuilder)
.HasColumnType("TEXT");

b.Property<byte[]>("ConcurrencyStamp")
.IsConcurrencyToken()
.IsRequired()
.HasColumnType("BLOB");

@@ -276,6 +281,7 @@ protected override void BuildModel(ModelBuilder modelBuilder)
.HasColumnType("TEXT");

b.Property<byte[]>("ConcurrencyStamp")
.IsConcurrencyToken()
.IsRequired()
.HasColumnType("BLOB");