newwer version
This commit is contained in:
@@ -1,67 +0,0 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Internal;
|
||||
using PoweredSoft.Data.Core;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Reflection;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace PoweredSoft.Data.EntityFrameworkCore
|
||||
{
|
||||
public class DbContextFactory : IDbContextFactory
|
||||
{
|
||||
private readonly DbContext _context;
|
||||
|
||||
public DbContextFactory(DbContext dbContext)
|
||||
{
|
||||
_context = dbContext;
|
||||
}
|
||||
|
||||
public void Add(object entity) => _context.Add(entity);
|
||||
|
||||
|
||||
|
||||
public IQueryable<T> GetQueryable<T>() where T : class => _context.Set<T>();
|
||||
|
||||
public IQueryable GetQueryable(Type type)
|
||||
{
|
||||
var setSource = (IDbSetSource)this._context.GetType().GetProperty("SetSource", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(_context);
|
||||
var ret = (IQueryable)((IDbSetCache)this).GetOrAddSet(setSource, type);
|
||||
return ret;
|
||||
}
|
||||
|
||||
public void Remove(object entity)
|
||||
{
|
||||
_context.Remove(entity);
|
||||
}
|
||||
|
||||
public int SaveChanges() => _context.SaveChanges();
|
||||
public async Task<int> SaveChangesAsync() => await _context.SaveChangesAsync();
|
||||
|
||||
public IEnumerable<PropertyInfo> GetKeyProperties(Type entityType)
|
||||
{
|
||||
var key = _context.Model.FindEntityType(entityType).FindPrimaryKey();
|
||||
var keysProperties = key.Properties.Select(t => t.PropertyInfo);
|
||||
return keysProperties;
|
||||
}
|
||||
|
||||
public IEnumerable<Expression<Func<TEntity, object>>> GetKeyProperties<TEntity>()
|
||||
{
|
||||
var keyProps = GetKeyProperties(typeof(TEntity));
|
||||
|
||||
var parameter = Expression.Parameter(typeof(TEntity));
|
||||
var result = keyProps
|
||||
.Select(keyProp =>
|
||||
{
|
||||
var property = Expression.Property(parameter, keyProp);
|
||||
var funcType = typeof(Expression<Func<TEntity, object>>);
|
||||
var lambda = Expression.Lambda(funcType, Expression.Convert(property, typeof(Object)), parameter);
|
||||
return (Expression<Func<TEntity, object>>)lambda;
|
||||
})
|
||||
.ToList();
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
using PoweredSoft.Data.Core;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace PoweredSoft.Data.EntityFrameworkCore
|
||||
{
|
||||
public class DbContextFactoryProvider : IDbContextFactoryProvider
|
||||
{
|
||||
private readonly IServiceProvider serviceProvider;
|
||||
|
||||
public DbContextFactoryProvider(IServiceProvider serviceProvider)
|
||||
{
|
||||
this.serviceProvider = serviceProvider;
|
||||
}
|
||||
|
||||
public IDbContextFactory GetContextFactory(Type contextType)
|
||||
{
|
||||
var dbContext = this.serviceProvider.GetRequiredService(contextType) as DbContext;
|
||||
var dbContextFactory = new DbContextFactory(dbContext);
|
||||
return dbContextFactory;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netcoreapp2.1</TargetFramework>
|
||||
<TargetFramework>netstandard2.1</TargetFramework>
|
||||
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
|
||||
<Copyright>Powered Softwares Inc.</Copyright>
|
||||
<PackageLicenseUrl>MIT</PackageLicenseUrl>
|
||||
@@ -9,7 +9,7 @@
|
||||
<RepositoryUrl>https://github.com/PoweredSoft/Data</RepositoryUrl>
|
||||
<RepositoryType>github</RepositoryType>
|
||||
<PackageTags>powered,soft,orm,db,context,ef,ef6,efcore,factory</PackageTags>
|
||||
<Version>1.1.3</Version>
|
||||
<Version>2.0.0</Version>
|
||||
<PackageIconUrl>https://secure.gravatar.com/avatar/4e32f73820c16718909a06c2927f1f8b?s=512&amp;r=g&amp;d=retro</PackageIconUrl>
|
||||
<Product>PoweredSoft.Data.EntityFrameworkCore</Product>
|
||||
<Description>the abstraction implementation for EF Core.</Description>
|
||||
@@ -21,7 +21,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.1.4" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -12,7 +12,6 @@ namespace PoweredSoft.Data.EntityFrameworkCore
|
||||
public static IServiceCollection AddPoweredSoftEntityFrameworkCoreDataServices(this IServiceCollection services)
|
||||
{
|
||||
services.AddPoweredSoftDataServices();
|
||||
services.TryAddTransient<IDbContextFactoryProvider, DbContextFactoryProvider>();
|
||||
services.AddTransient<IAsyncQueryableHandlerService, AsyncQueryableHandlerService>();
|
||||
return services;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user