Browse Source

Try to set GUID Id in the InsertAsync method of the EF Core repository.

Resolve #4631
pull/4634/head
maliming 6 years ago
parent
commit
58f0204e7b
  1. 38
      framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Domain/Repositories/EntityFrameworkCore/EfCoreRepository.cs
  2. 1
      framework/src/Volo.Abp.MongoDB/Volo/Abp/Domain/Repositories/MongoDB/MongoDbRepository.cs

38
framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Domain/Repositories/EntityFrameworkCore/EfCoreRepository.cs

@ -10,6 +10,7 @@ using Microsoft.Extensions.Options;
using Volo.Abp.Domain.Entities; using Volo.Abp.Domain.Entities;
using Volo.Abp.EntityFrameworkCore; using Volo.Abp.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore.DependencyInjection; using Volo.Abp.EntityFrameworkCore.DependencyInjection;
using Volo.Abp.Guids;
namespace Volo.Abp.Domain.Repositories.EntityFrameworkCore namespace Volo.Abp.Domain.Repositories.EntityFrameworkCore
{ {
@ -28,9 +29,12 @@ namespace Volo.Abp.Domain.Repositories.EntityFrameworkCore
private readonly IDbContextProvider<TDbContext> _dbContextProvider; private readonly IDbContextProvider<TDbContext> _dbContextProvider;
private readonly Lazy<AbpEntityOptions<TEntity>> _entityOptionsLazy; private readonly Lazy<AbpEntityOptions<TEntity>> _entityOptionsLazy;
protected virtual IGuidGenerator GuidGenerator { get; set; }
public EfCoreRepository(IDbContextProvider<TDbContext> dbContextProvider) public EfCoreRepository(IDbContextProvider<TDbContext> dbContextProvider)
{ {
_dbContextProvider = dbContextProvider; _dbContextProvider = dbContextProvider;
GuidGenerator = SimpleGuidGenerator.Instance;
_entityOptionsLazy = new Lazy<AbpEntityOptions<TEntity>>( _entityOptionsLazy = new Lazy<AbpEntityOptions<TEntity>>(
() => ServiceProvider () => ServiceProvider
@ -39,9 +43,11 @@ namespace Volo.Abp.Domain.Repositories.EntityFrameworkCore
.GetOrNull<TEntity>() ?? AbpEntityOptions<TEntity>.Empty .GetOrNull<TEntity>() ?? AbpEntityOptions<TEntity>.Empty
); );
} }
public override async Task<TEntity> InsertAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default) public override async Task<TEntity> InsertAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default)
{ {
CheckAndSetId(entity);
var savedEntity = DbSet.Add(entity).Entity; var savedEntity = DbSet.Add(entity).Entity;
if (autoSave) if (autoSave)
@ -65,7 +71,7 @@ namespace Volo.Abp.Domain.Repositories.EntityFrameworkCore
return updatedEntity; return updatedEntity;
} }
public override async Task DeleteAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default) public override async Task DeleteAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default)
{ {
DbSet.Remove(entity); DbSet.Remove(entity);
@ -94,7 +100,7 @@ namespace Volo.Abp.Domain.Repositories.EntityFrameworkCore
} }
public override async Task<TEntity> FindAsync( public override async Task<TEntity> FindAsync(
Expression<Func<TEntity, bool>> predicate, Expression<Func<TEntity, bool>> predicate,
bool includeDetails = true, bool includeDetails = true,
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default)
{ {
@ -172,16 +178,38 @@ namespace Volo.Abp.Domain.Repositories.EntityFrameworkCore
return query; return query;
} }
protected virtual void CheckAndSetId(TEntity entity)
{
if (entity is IEntity<Guid> entityWithGuidId)
{
TrySetGuidId(entity, entityWithGuidId);
}
}
protected virtual void TrySetGuidId(TEntity entry, IEntity<Guid> entity)
{
if (entity.Id != default)
{
return;
}
EntityHelper.TrySetId(
entity,
() => GuidGenerator.Create(),
true
);
}
} }
public class EfCoreRepository<TDbContext, TEntity, TKey> : EfCoreRepository<TDbContext, TEntity>, public class EfCoreRepository<TDbContext, TEntity, TKey> : EfCoreRepository<TDbContext, TEntity>,
IEfCoreRepository<TEntity, TKey>, IEfCoreRepository<TEntity, TKey>,
ISupportsExplicitLoading<TEntity, TKey> ISupportsExplicitLoading<TEntity, TKey>
where TDbContext : IEfCoreDbContext where TDbContext : IEfCoreDbContext
where TEntity : class, IEntity<TKey> where TEntity : class, IEntity<TKey>
{ {
public EfCoreRepository(IDbContextProvider<TDbContext> dbContextProvider) public EfCoreRepository(IDbContextProvider<TDbContext> dbContextProvider)
: base(dbContextProvider) : base(dbContextProvider)
{ {

1
framework/src/Volo.Abp.MongoDB/Volo/Abp/Domain/Repositories/MongoDB/MongoDbRepository.cs

@ -49,6 +49,7 @@ namespace Volo.Abp.Domain.Repositories.MongoDB
LocalEventBus = NullLocalEventBus.Instance; LocalEventBus = NullLocalEventBus.Instance;
DistributedEventBus = NullDistributedEventBus.Instance; DistributedEventBus = NullDistributedEventBus.Instance;
EntityChangeEventHelper = NullEntityChangeEventHelper.Instance; EntityChangeEventHelper = NullEntityChangeEventHelper.Instance;
GuidGenerator = SimpleGuidGenerator.Instance;
} }
public override async Task<TEntity> InsertAsync( public override async Task<TEntity> InsertAsync(

Loading…
Cancel
Save