Skip to content

Commit db6cb8b

Browse files
committed
feature:
修复报错问题
1 parent 4affb11 commit db6cb8b

File tree

5 files changed

+21
-23
lines changed

5 files changed

+21
-23
lines changed

sample/Module.Sample/DbContexts/ModuleDbContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace Module.Sample.DbContexts
66
{
77
public class ModuleDbContext : LuckDbContextBase
88
{
9-
public ModuleDbContext(DbContextOptions options, IServiceProvider serviceProvider) : base(options, serviceProvider)
9+
public ModuleDbContext(DbContextOptions options, IServiceProvider serviceProvider) : base(options)
1010
{
1111
}
1212
public DbSet<Order> Orders => Set<Order>();

src/framework/Luck.EntityFrameworkCore/DbContexts/LuckDbContextBase.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,9 @@ namespace Luck.EntityFrameworkCore.DbContexts
88
/// <summary>
99
/// 类类上下文
1010
/// </summary>
11-
public abstract class LuckDbContextBase(DbContextOptions options, IServiceProvider serviceProvider)
11+
public abstract class LuckDbContextBase(DbContextOptions options)
1212
: DbContext(options),ILuckDbContext
1313
{
14-
protected IServiceProvider ServiceProvider { get; set; } =
15-
Check.NotNull(serviceProvider, nameof(serviceProvider));
16-
1714
protected override void OnModelCreating(ModelBuilder modelBuilder)
1815
{
1916
base.OnModelCreating(modelBuilder);

src/framework/Luck.EntityFrameworkCore/Repositories/EFCoreAggregateRootRepository.cs

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,57 +13,53 @@ public class EfCoreAggregateRootRepository<TEntity, TKey> : AggregateRootReposit
1313
where TKey : IEquatable<TKey>
1414
{
1515
private readonly IDbContextFactory<LuckDbContextBase> _dbContextFactory;
16-
protected LuckDbContextBase DbContext { get; }
17-
private readonly IUnitOfWork _unitOfWork;
16+
private readonly LuckDbContextBase _luckDbContextBase;
17+
protected LuckDbContextBase DbContext => _luckDbContextBase;
1818

1919
public EfCoreAggregateRootRepository(IUnitOfWork unitOfWork)
2020
{
21-
_unitOfWork = unitOfWork;
22-
DbContext = unitOfWork as LuckDbContextBase ?? throw new ArgumentNullException(nameof(ILuckDbContext));
21+
_luckDbContextBase = unitOfWork.GetLuckDbContext() as LuckDbContextBase ??
22+
throw new ArgumentNullException(nameof(ILuckDbContext));
2323
}
2424

2525
public override void Add(TEntity entity)
2626
{
27-
DbContext.Add(entity);
27+
_luckDbContextBase.Add(entity);
2828
}
2929

30-
3130
public override void Attach(TEntity entity)
3231
{
33-
DbContext.Attach(entity);
32+
_luckDbContextBase.Attach(entity);
3433
}
3534

36-
3735
public override void Update(TEntity entity)
3836
{
39-
DbContext.Update(entity);
37+
_luckDbContextBase.Update(entity);
4038
}
4139

42-
4340
public override void Remove(TEntity entity)
4441
{
45-
DbContext.Remove(entity);
42+
_luckDbContextBase.Remove(entity);
4643
}
4744

48-
4945
public override TEntity? Find(TKey primaryKey)
5046
{
51-
return DbContext.Find<TEntity>(primaryKey);
47+
return _luckDbContextBase.Find<TEntity>(primaryKey);
5248
}
5349

5450
public override ValueTask<TEntity?> FindAsync(TKey primaryKey)
5551
{
56-
return DbContext.FindAsync<TEntity>(primaryKey);
52+
return _luckDbContextBase.FindAsync<TEntity>(primaryKey);
5753
}
5854

5955
public override Task<TEntity?> FindAsync(Expression<Func<TEntity, bool>> predicate)
6056
{
61-
return DbContext.Set<TEntity>().FirstOrDefaultAsync(predicate);
57+
return _luckDbContextBase.Set<TEntity>().FirstOrDefaultAsync(predicate);
6258
}
6359

6460
protected override IQueryable<TEntity> FindQueryable()
6561
{
66-
return DbContext.Set<TEntity>();
62+
return _luckDbContextBase.Set<TEntity>();
6763
}
6864
}
6965
}

test/Luck.UnitTest/EntityFrameworkCore_Tests/EntityFrameworkCoreTests.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,13 @@ public async Task AggregateRootRepositoryAddAsync()
4242
{
4343
try
4444
{
45-
_orderAggregateRootRepository.Add(new Order("Pual", "Los"));
45+
var order = new Order("Pual", "Los");
46+
_orderAggregateRootRepository.Add(order);
4647
await _unitOfWork.CommitAsync();
48+
49+
var result = await _orderAggregateRootRepository.FindAsync(x => x.Name == "Pual");
50+
Assert.NotNull(result);
51+
Assert.True(result.Name == order.Name);
4752
}
4853
catch (Exception e)
4954
{

test/Luck.UnitTest/EntityFrameworkCore_Tests/TestContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace Luck.UnitTest.EntityFrameworkCore_Tests;
77

88
public class TestContext : LuckDbContextBase
99
{
10-
public TestContext(DbContextOptions options, IServiceProvider serviceProvider) : base(options, serviceProvider)
10+
public TestContext(DbContextOptions options) : base(options)
1111
{
1212
}
1313

0 commit comments

Comments
 (0)