支持abp.vnext
This commit is contained in:
parent
f45f76e4e5
commit
229f6b2d8c
|
@ -6,11 +6,16 @@ using ShardingCore.Sharding.Abstractions;
|
||||||
using ShardingCore.Sharding.ShardingDbContextExecutors;
|
using ShardingCore.Sharding.ShardingDbContextExecutors;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.ComponentModel.DataAnnotations.Schema;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using ShardingCore.EFCores.OptionsExtensions;
|
using ShardingCore.EFCores.OptionsExtensions;
|
||||||
|
using ShardingCore.Helpers;
|
||||||
|
using ShardingCore.Utils;
|
||||||
|
using Volo.Abp.Domain.Entities;
|
||||||
using Volo.Abp.EntityFrameworkCore;
|
using Volo.Abp.EntityFrameworkCore;
|
||||||
|
using Volo.Abp.Reflection;
|
||||||
|
|
||||||
namespace Samples.AbpSharding
|
namespace Samples.AbpSharding
|
||||||
{
|
{
|
||||||
|
@ -77,9 +82,45 @@ namespace Samples.AbpSharding
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public DbContext CreateGenericDbContext<TEntity>(TEntity entity) where TEntity : class
|
public DbContext CreateGenericDbContext<TEntity>(TEntity entity) where TEntity : class
|
||||||
{
|
{
|
||||||
|
CheckAndSetShardingKeyThatSupportAutoCreate(entity);
|
||||||
return _shardingDbContextExecutor.CreateGenericDbContext(entity);
|
return _shardingDbContextExecutor.CreateGenericDbContext(entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void CheckAndSetShardingKeyThatSupportAutoCreate<TEntity>(TEntity entity) where TEntity : class
|
||||||
|
{
|
||||||
|
if (entity is IShardingKeyIsGuId)
|
||||||
|
{
|
||||||
|
|
||||||
|
if (entity is IEntity<Guid> guidEntity)
|
||||||
|
{
|
||||||
|
if (guidEntity.Id != default)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
var idProperty = entity.GetProperty(nameof(IEntity<Guid>.Id));
|
||||||
|
|
||||||
|
var dbGeneratedAttr = ReflectionHelper
|
||||||
|
.GetSingleAttributeOrDefault<DatabaseGeneratedAttribute>(
|
||||||
|
idProperty
|
||||||
|
);
|
||||||
|
|
||||||
|
if (dbGeneratedAttr != null && dbGeneratedAttr.DatabaseGeneratedOption != DatabaseGeneratedOption.None)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
EntityHelper.TrySetId(
|
||||||
|
guidEntity,
|
||||||
|
() => GuidGenerator.Create(),
|
||||||
|
true
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}else if (entity is IShardingKeyIsCreationTime)
|
||||||
|
{
|
||||||
|
AuditPropertySetter?.SetCreationProperties(entity);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public override EntityEntry Add(object entity)
|
public override EntityEntry Add(object entity)
|
||||||
{
|
{
|
||||||
|
|
|
@ -0,0 +1,11 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Samples.AbpSharding
|
||||||
|
{
|
||||||
|
public interface IShardingKeyIsGuId
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,11 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Samples.AbpSharding
|
||||||
|
{
|
||||||
|
public interface IShardingKeyIsCreationTime
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
|
@ -72,6 +72,10 @@ namespace ShardingCore.Extensions
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
public static PropertyInfo GetProperty(this object obj, string propertyName)
|
||||||
|
{
|
||||||
|
return obj.GetType().GetProperty(propertyName, _bindingFlags);
|
||||||
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 类型X是否包含某个属性
|
/// 类型X是否包含某个属性
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
Loading…
Reference in New Issue