提交 f283a1bb 编写于 作者: S simon

增加数据访问,api,实体映射等,可以运行,接口测试不正确

上级 343bde14
using System;
using Aurora.Core.Domain.Entities;
using Aurora.Core.Domain.Response;
namespace Aurora.Core.Domain.xxx
{
public class CategoryProfile : Profile
{
protected override void Configure()
{
CreateMap<CategoryDto, Category>();
}
}
}
using System;
using Aurora.Core.Domain.Entities;
using Aurora.Core.Domain.Response;
namespace Aurora.Core.Domain.xxx
{
public class CategoryProfile : Profile
{
protected override void Configure()
{
CreateMap<CategoryDto, Category>();
}
}
}
using System;
using Aurora.Core.Domain.Entities;
using Aurora.Core.Domain.Response;
namespace Aurora.Core.Domain.AutomapperProfiles
{
public class CategoryProfile : Profile
{
protected override void Configure()
{
CreateMap<CategoryDto, Category>();
}
}
}
using System;
namespace Aurora.Core.Domain.Response
{
public class CategoryDto
{
}
}
using System;
namespace Aurora.Core.Domain.xxx
{
public class xxx
{
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Aurora.Common.Entities;
using Aurora.Core.Domain.Entities;
using Aurora.Infrastructure.Tenant;
using Aurora.Infrastructure.User;
using Microsoft.EntityFrameworkCore;
namespace Aurora.Core.EntityFramework
{
public class ApplicationDbContext : DbContext
{
public static string ConnectionString { get; set; }
private readonly ICurrentUser currentUser;
private readonly ICurrentTenant currentTenant;
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options,
ICurrentUserProvider currentUserProvider,
ICurrentTenantProvider currentTenantProvider) : base(options)
{
currentUser = currentUserProvider.GetCurrentUser();
currentTenant = currentTenantProvider.GetCurrentTenant();
}
public DbSet<Category> Categories { get; set; }
protected override void OnModelCreating(ModelBuilder builder)
{
//多租户过滤
foreach (var type in GetBaseEntityTypes())
{
var method = SetGlobalQueryMethod.MakeGenericMethod(type);
method.Invoke(this, new object[] { builder });
}
builder.Entity<Category>().HasKey(t => new { t.TenantCode });
// builder.Entity<Category>().HasIndex(p => p.TenantCode);
base.OnModelCreating(builder);
}
private static IList<Type> _baseEntityTypesCache;
private static IList<Type> GetBaseEntityTypes()
{
if (_baseEntityTypesCache != null)
return _baseEntityTypesCache.ToList();
_baseEntityTypesCache = (from t in typeof(TenantEntityBase).GetTypeInfo().Assembly.DefinedTypes
where t.BaseType == typeof(TenantEntityBase)
select t.AsType()).ToList();
return _baseEntityTypesCache;
}
public override int SaveChanges()
{
UpdateCommonFileds();
return base.SaveChanges();
}
public override async Task<int> SaveChangesAsync(CancellationToken cancellationToken = default(CancellationToken))
{
UpdateCommonFileds();
return await base.SaveChangesAsync(cancellationToken);
}
private void UpdateCommonFileds()
{
var nowTime = DateTime.Now;
if (string.IsNullOrEmpty(currentUser.UserName))
{
var tcode = currentTenant.TenantCode;
}
foreach (var entry in this.ChangeTracker.Entries<TenantEntityBase>().Where(x => x.State == EntityState.Added || x.State == EntityState.Modified))
{
var entity = entry.Entity;
switch (entry.State)
{
case EntityState.Added:
if (entity.TenantCode == "")
entity.TenantCode = currentTenant.TenantCode;
entity.CreateTime = nowTime;
entity.CreateUserName = currentUser.UserName;
break;
case EntityState.Modified:
entity.ModifyTime = nowTime;
entity.ModifyUserName = currentUser.UserName;
break;
}
}
this.ChangeTracker.DetectChanges();
}
static readonly MethodInfo SetGlobalQueryMethod = typeof(ApplicationDbContext).GetMethods(BindingFlags.Public | BindingFlags.Instance)
.Single(t => t.IsGenericMethod && t.Name == "SetGlobalQuery");
public void SetGlobalQuery<T>(ModelBuilder builder) where T : TenantEntityBase
{
builder.Entity<T>().HasQueryFilter(e => e.TenantCode == currentTenant.TenantCode);
}
}
}
\ No newline at end of file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Aurora.Common.Entities;
using Aurora.Core.Domain.Entities;
using Aurora.Infrastructure.Tenant;
using Aurora.Infrastructure.User;
using Microsoft.EntityFrameworkCore;
namespace Aurora.Core.EntityFramework
{
public class ApplicationDbContext : DbContext
{
public static string ConnectionString { get; set; }
private readonly ICurrentUser currentUser;
private readonly ICurrentTenant currentTenant;
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options,
ICurrentUserProvider currentUserProvider,
ICurrentTenantProvider currentTenantProvider) : base(options)
{
currentUser = currentUserProvider.GetCurrentUser();
currentTenant = currentTenantProvider.GetCurrentTenant();
}
public DbSet<Category> Categories { get; set; }
protected override void OnModelCreating(ModelBuilder builder)
{
//多租户过滤
foreach (var type in GetBaseEntityTypes())
{
var method = SetGlobalQueryMethod.MakeGenericMethod(type);
method.Invoke(this, new object[] { builder });
}
builder.Entity<Category>().HasKey(t => new { t.TenantCode });
// builder.Entity<Category>().HasIndex(p => p.TenantCode);
base.OnModelCreating(builder);
}
private static IList<Type> _baseEntityTypesCache;
private static IList<Type> GetBaseEntityTypes()
{
if (_baseEntityTypesCache != null)
return _baseEntityTypesCache.ToList();
_baseEntityTypesCache = (from t in typeof(TenantEntityBase).GetTypeInfo().Assembly.DefinedTypes
where t.BaseType == typeof(TenantEntityBase)
select t.AsType()).ToList();
return _baseEntityTypesCache;
}
public override int SaveChanges()
{
UpdateCommonFileds();
return base.SaveChanges();
}
public override async Task<int> SaveChangesAsync(CancellationToken cancellationToken = default(CancellationToken))
{
UpdateCommonFileds();
return await base.SaveChangesAsync(cancellationToken);
}
private void UpdateCommonFileds()
{
var nowTime = DateTime.Now;
if (string.IsNullOrEmpty(currentUser.UserName))
{
var tcode = currentTenant.TenantCode;
}
foreach (var entry in this.ChangeTracker.Entries<TenantEntityBase>().Where(x => x.State == EntityState.Added || x.State == EntityState.Modified))
{
var entity = entry.Entity;
switch (entry.State)
{
case EntityState.Added:
if (entity.TenantCode == "")
entity.TenantCode = currentTenant.TenantCode;
entity.CreateTime = nowTime;
entity.CreateUserName = currentUser.UserName;
break;
case EntityState.Modified:
entity.ModifyTime = nowTime;
entity.ModifyUserName = currentUser.UserName;
break;
}
}
this.ChangeTracker.DetectChanges();
}
static readonly MethodInfo SetGlobalQueryMethod = typeof(ApplicationDbContext).GetMethods(BindingFlags.Public | BindingFlags.Instance)
.Single(t => t.IsGenericMethod && t.Name == "SetGlobalQuery");
public void SetGlobalQuery<T>(ModelBuilder builder) where T : TenantEntityBase
{
builder.Entity<T>().HasQueryFilter(e => e.TenantCode == currentTenant.TenantCode);
}
}
}
\ No newline at end of file
using System;
using Aurora.Core.Domain.Entities;
namespace Aurora.Core.Domain.Dto
{
public class CategoryDto : Category
{
}
}
using System;
namespace Aurora.Core.IService
{
public class ICategoryService
{
}
}
using System;
namespace Aurora.Core.Service
{
public class Class1
{
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册