Commit fefafb4b authored by 阿星Plus's avatar 阿星Plus

修复 Plus 和 Plus.EntitiyFramework 出错问题

parent 55a8ade7
using Microsoft.EntityFrameworkCore;
using Plus.Dependency;
using Plus.Domain.Entities;
using Plus.Reflection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace Plus.EntityFramework
{
internal class EfCoreDbContextEntityFinder : IDbContextEntityFinder, ITransientDependency
{
public IEnumerable<EntityTypeInfo> GetEntityTypeInfos(Type dbContextType)
{
return
from property in dbContextType.GetProperties(BindingFlags.Public | BindingFlags.Instance)
where
ReflectionHelper.IsAssignableToGenericType(property.PropertyType, typeof(DbSet<>)) &&
ReflectionHelper.IsAssignableToGenericType(property.PropertyType.GenericTypeArguments[0], typeof(IEntity<>))
select new EntityTypeInfo(property.PropertyType.GenericTypeArguments[0], property.DeclaringType);
}
}
}
\ No newline at end of file
......@@ -17,7 +17,7 @@
<PackageLicenseUrl>https://raw.githubusercontent.com/Meowv/.netcoreplus/master/LICENSE</PackageLicenseUrl>
<PackageTags>plus;plus.entityframework;.netcoreplus;</PackageTags>
<PackageReleaseNotes>Plus.EntityFramework</PackageReleaseNotes>
<Version>1.0.0</Version>
<Version>1.0.0.1</Version>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
......
using Castle.Core;
using Castle.Core.Logging;
using Plus.Dependency;
using System.Threading;
namespace Plus.Domain.Uow
{
public class AsyncLocalCurrentUnitOfWorkProvider : ICurrentUnitOfWorkProvider, ITransientDependency
{
private class LocalUowWrapper
{
public IUnitOfWork UnitOfWork { get; set; }
public LocalUowWrapper(IUnitOfWork unitOfWork)
{
UnitOfWork = unitOfWork;
}
}
private static readonly AsyncLocal<LocalUowWrapper> AsyncLocalUow = new AsyncLocal<LocalUowWrapper>();
[DoNotWire]
public IUnitOfWork Current
{
get
{
return GetCurrentUow();
}
set
{
SetCurrentUow(value);
}
}
public ILogger Logger { get; set; }
public AsyncLocalCurrentUnitOfWorkProvider()
{
Logger = NullLogger.Instance;
}
private static IUnitOfWork GetCurrentUow()
{
IUnitOfWork unitOfWork = AsyncLocalUow.Value?.UnitOfWork;
if (unitOfWork == null)
{
return null;
}
if (unitOfWork.IsDisposed)
{
AsyncLocalUow.Value = null;
return null;
}
return unitOfWork;
}
private static void SetCurrentUow(IUnitOfWork value)
{
lock (AsyncLocalUow)
{
if (value == null)
{
if (AsyncLocalUow.Value != null)
{
if (AsyncLocalUow.Value.UnitOfWork?.Outer == null)
{
AsyncLocalUow.Value.UnitOfWork = null;
AsyncLocalUow.Value = null;
}
else
{
AsyncLocalUow.Value.UnitOfWork = AsyncLocalUow.Value.UnitOfWork.Outer;
}
}
}
else if (AsyncLocalUow.Value?.UnitOfWork == null)
{
if (AsyncLocalUow.Value != null)
{
AsyncLocalUow.Value.UnitOfWork = value;
}
AsyncLocalUow.Value = new LocalUowWrapper(value);
}
else
{
value.Outer = AsyncLocalUow.Value.UnitOfWork;
AsyncLocalUow.Value.UnitOfWork = value;
}
}
}
}
}
\ No newline at end of file
......@@ -17,7 +17,7 @@
<PackageLicenseUrl>https://raw.githubusercontent.com/Meowv/.netcoreplus/master/LICENSE</PackageLicenseUrl>
<PackageTags>plus;.netcoreplus;</PackageTags>
<PackageReleaseNotes>Plus</PackageReleaseNotes>
<Version>1.0.0</Version>
<Version>1.0.0.1</Version>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment