提交 07ee39d6 编写于 作者: lwplvx's avatar lwplvx

Microsoft.EntityFrameworkCore 降级到 Version="3.1.15" ,因为 The database provider...

Microsoft.EntityFrameworkCore 降级到 Version="3.1.15" ,因为 The database provider attempted to register an implementation of the 'IRelationalTypeMappingSource' service. 错误
上级 1213d9cb
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
......@@ -7,8 +7,8 @@
<ItemGroup>
<PackageReference Include="Automapper" Version="10.1.1" />
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="8.1.1" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.6" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.15" />
<PackageReference Include="MySql.Data.EntityFrameworkCore" Version="8.0.22" />
</ItemGroup>
......
......@@ -16,56 +16,56 @@ using Microsoft.EntityFrameworkCore;
namespace Aurora.Core.EntityFramework
{
public class ApplicationDbContext : ApplicationReadonlyDbContext
public class ApplicationDbContext : DbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
private readonly ICurrentUser currentUser;
private readonly ICurrentTenant currentTenant;
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
{
// ,
// ICurrentUserProvider currentUserProvider,
// ICurrentTenantProvider currentTenantProvider
// currentUser = currentUserProvider.GetCurrentUser();
// currentTenant = currentTenantProvider.GetCurrentTenant();
}
public override int SaveChanges()
public DbSet<Category> Categories { get; set; }
protected override void OnModelCreating(ModelBuilder builder)
{
UpdateCommonFileds();
return base.SaveChanges();
//多租户过滤
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);
}
public override async Task<int> SaveChangesAsync(CancellationToken cancellationToken = default(CancellationToken))
private static IList<Type> _baseEntityTypesCache;
private static IList<Type> GetBaseEntityTypes()
{
UpdateCommonFileds();
return await base.SaveChangesAsync(cancellationToken);
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;
}
protected static readonly MethodInfo SetGlobalQueryMethod = typeof(ApplicationDbContext).GetMethods(BindingFlags.Public | BindingFlags.Instance)
.Single(t => t.IsGenericMethod && t.Name == "SetGlobalQuery");
private void UpdateCommonFileds()
public void SetGlobalQuery<T>(ModelBuilder builder) where T : TenantEntityBase
{
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();
builder.Entity<T>().HasQueryFilter(e => e.TenantCode == currentTenant.TenantCode);
}
}
}
\ No newline at end of file
......@@ -18,8 +18,8 @@ namespace Aurora.Core.EntityFramework
{
public class ApplicationReadonlyDbContext : DbContext
{
protected readonly ICurrentUser currentUser;
protected readonly ICurrentTenant currentTenant;
private readonly ICurrentUser currentUser;
private readonly ICurrentTenant currentTenant;
public ApplicationReadonlyDbContext(DbContextOptions<ApplicationReadonlyDbContext> options) : base(options)
{
......
......@@ -6,8 +6,8 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="5.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.6" />
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="5.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.15" />
<PackageReference Include="MySql.Data.EntityFrameworkCore" Version="8.0.22" />
</ItemGroup>
<PropertyGroup>
......
......@@ -2,14 +2,12 @@
<ItemGroup>
<ProjectReference Include="..\Aurora.Core.IService\Aurora.Core.IService.csproj" />
<ProjectReference Include="..\..\..\Aurora.Infrastructure\Aurora.Infrastructure.csproj" />
<ProjectReference Include="..\Aurora.Core.EntityFramework\Aurora.Core.EntityFramework.csproj" />
<ProjectReference Include="..\..\..\Aurora.Infrastructure\Aurora.Domain.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Automapper" Version="10.1.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.6" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.15" />
</ItemGroup>
<PropertyGroup>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册