Skip to content

Commit 4084e69

Browse files
committed
fix(EntityFramework): Fix the problem that Creator and Modifier cannot be assigned values
1 parent c30a580 commit 4084e69

File tree

5 files changed

+59
-4
lines changed

5 files changed

+59
-4
lines changed

src/Data/Masa.Contrib.Data.Contracts.EF/DataFiltering/SoftDeleteSaveChangesFilter.cs

+15-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ public class SoftDeleteSaveChangesFilter<TDbContext, TUserId> : ISaveChangesFilt
77
where TDbContext : DbContext
88
where TUserId : IComparable
99
{
10+
private readonly Type _userIdType;
1011
private readonly IUserContext? _userContext;
1112
private readonly TDbContext _context;
1213
private readonly MasaDbContextOptions<TDbContext> _masaDbContextOptions;
@@ -16,6 +17,7 @@ public SoftDeleteSaveChangesFilter(
1617
TDbContext dbContext,
1718
IUserContext? userContext = null)
1819
{
20+
_userIdType = typeof(TUserId);
1921
_masaDbContextOptions = masaDbContextOptions;
2022
_context = dbContext;
2123
_userContext = userContext;
@@ -38,7 +40,7 @@ public void OnExecuting(ChangeTracker changeTracker)
3840
if (_userContext != null && entity.Entity is IAuditEntity<TUserId> &&
3941
entity.CurrentValues[nameof(IAuditEntity<TUserId>.Modifier)] != default)
4042
{
41-
var userId = _userContext.UserId;
43+
var userId = GetUserId(_userContext.UserId);
4244
if (userId != null) entity.CurrentValues[nameof(IAuditEntity<TUserId>.Modifier)] = userId;
4345

4446
entity.CurrentValues[nameof(IAuditEntity<TUserId>.ModificationTime)] =
@@ -77,4 +79,16 @@ protected virtual void HandleDependent(object dependentEntry)
7779
if (entityEntry.Entity is ISoftDelete)
7880
entityEntry.CurrentValues[nameof(ISoftDelete.IsDeleted)] = true;
7981
}
82+
83+
private object? GetUserId(string? userId)
84+
{
85+
if (userId == null)
86+
return null;
87+
88+
if (_userIdType == typeof(Guid))
89+
{
90+
return Guid.Parse(userId);
91+
}
92+
return Convert.ChangeType(userId, _userIdType);
93+
}
8094
}

src/Data/Masa.Contrib.Data.EntityFrameworkCore/Filters/SaveChangeFilter.cs

+15-1
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ namespace Masa.Contrib.Data.EntityFrameworkCore.Filters;
66
public class SaveChangeFilter<TDbContext, TUserId> : ISaveChangesFilter
77
where TDbContext : DbContext
88
{
9+
private readonly Type _userIdType;
910
private readonly IUserContext? _userContext;
1011

1112
public SaveChangeFilter(IUserContext? userContext = null)
1213
{
14+
_userIdType = typeof(TUserId);
1315
_userContext = userContext;
1416
}
1517

@@ -21,7 +23,7 @@ public void OnExecuting(ChangeTracker changeTracker)
2123
.Where(entry => entry.Entity is IAuditEntity<TUserId> &&
2224
(entry.State == EntityState.Added || entry.State == EntityState.Modified)))
2325
{
24-
var userId = _userContext?.UserId;
26+
var userId = GetUserId(_userContext?.UserId);
2527
if (entity.State == EntityState.Added)
2628
{
2729
if (userId != null)
@@ -40,4 +42,16 @@ public void OnExecuting(ChangeTracker changeTracker)
4042
DateTime.UtcNow; //The current time to change to localization after waiting for localization
4143
}
4244
}
45+
46+
private object? GetUserId(string? userId)
47+
{
48+
if (userId == null)
49+
return null;
50+
51+
if (_userIdType == typeof(Guid))
52+
{
53+
return Guid.Parse(userId);
54+
}
55+
return Convert.ChangeType(userId, _userIdType);
56+
}
4357
}

src/Data/Masa.Contrib.Data.EntityFrameworkCore/ServiceCollectionExtensions.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ private static IServiceCollection AddCoreServices<TDbContextImplementation, TUse
6767
serviceProvider => serviceProvider.GetRequiredService<MasaDbContextOptions<TDbContextImplementation>>(),
6868
optionsLifetime));
6969

70-
services.TryAdd(new ServiceDescriptor(typeof(ISaveChangesFilter), typeof(SaveChangeFilter<TDbContextImplementation, TUserId>), ServiceLifetime.Scoped));
70+
services.TryAddEnumerable(new ServiceDescriptor(typeof(ISaveChangesFilter), typeof(SaveChangeFilter<TDbContextImplementation, TUserId>), ServiceLifetime.Scoped));
7171
return services;
7272
}
7373

test/Masa.Contrib.Data.EntityFrameworkCore.Tests/DbContextTest.cs

+26
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,32 @@ public async Task TestDisabledSoftDelete()
138138
}
139139
}
140140

141+
[TestMethod]
142+
public void TestAddMultiMasaDbContextReturnSaveChangeFilterEqual1()
143+
{
144+
var services = new ServiceCollection();
145+
services.AddMasaDbContext<CustomizeDbContext>()
146+
.AddMasaDbContext<CustomizeDbContext>();
147+
148+
var serviceProvider = services.BuildServiceProvider();
149+
Assert.IsTrue(serviceProvider.GetServices<ISaveChangesFilter>().Count() == 1);
150+
}
151+
152+
[TestMethod]
153+
public void TestAddMasaDbContextReturnSaveChangeFilterEqual2()
154+
{
155+
var services = new ServiceCollection();
156+
services.AddMasaDbContext<CustomizeDbContext>(opt =>
157+
{
158+
opt.UseSqlite(Guid.NewGuid().ToString()).UseFilter();
159+
});
160+
161+
var serviceProvider = services.BuildServiceProvider();
162+
163+
var filters = serviceProvider.GetServices<ISaveChangesFilter>();
164+
Assert.IsTrue(filters.Count() == 2);
165+
}
166+
141167
[TestMethod]
142168
public async Task TestGetPaginatedListAsyncReturnCountEqualResultCount()
143169
{

test/Masa.Contrib.Data.EntityFrameworkCore.Tests/_Imports.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
global using Masa.BuildingBlocks.Data.Contracts.DataFiltering;
66
global using Masa.BuildingBlocks.Data.Contracts.Paginated;
77
global using Masa.Contrib.Data.Contracts.EF;
8+
global using Masa.Contrib.Data.EntityFrameworkCore.Filters;
9+
global using Masa.Contrib.Data.EntityFrameworkCore.Sqlite;
810
global using Masa.Contrib.Data.EntityFrameworkCore.Tests.Internal;
911
global using Masa.Contrib.Data.EntityFrameworkCore.Tests.Models;
1012
global using Microsoft.EntityFrameworkCore;
1113
global using Microsoft.Extensions.DependencyInjection;
1214
global using Microsoft.Extensions.Options;
1315
global using Microsoft.VisualStudio.TestTools.UnitTesting;
14-
global using Masa.Contrib.Data.EntityFrameworkCore.Sqlite;

0 commit comments

Comments
 (0)