2021-08-23 19:30:52 +08:00
|
|
|
|
using System;
|
2022-07-20 22:15:05 +08:00
|
|
|
|
using System.Linq.Expressions;
|
|
|
|
|
using System.Reflection;
|
2021-08-23 19:30:52 +08:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2022-07-20 22:15:05 +08:00
|
|
|
|
using Microsoft.EntityFrameworkCore.Metadata;
|
|
|
|
|
using Sample.MySql.Domain.Entities;
|
2021-08-23 19:30:52 +08:00
|
|
|
|
using Sample.MySql.Domain.Maps;
|
2021-10-11 20:58:55 +08:00
|
|
|
|
using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions;
|
2021-08-23 19:30:52 +08:00
|
|
|
|
using ShardingCore.Sharding;
|
2021-10-11 20:58:55 +08:00
|
|
|
|
using ShardingCore.Sharding.Abstractions;
|
2021-08-23 19:30:52 +08:00
|
|
|
|
|
|
|
|
|
namespace Sample.MySql.DbContexts
|
|
|
|
|
{
|
2022-11-01 13:46:52 +08:00
|
|
|
|
public class DefaultShardingDbContext : AbstractShardingDbContext, IShardingTableDbContext
|
2021-08-23 19:30:52 +08:00
|
|
|
|
{
|
2022-11-16 19:57:29 +08:00
|
|
|
|
public DbSet<DynamicTable> DynamicTables { get; set; }
|
2021-08-23 19:30:52 +08:00
|
|
|
|
public DefaultShardingDbContext(DbContextOptions<DefaultShardingDbContext> options) : base(options)
|
|
|
|
|
{
|
2022-01-18 11:22:12 +08:00
|
|
|
|
//切记不要在构造函数中使用会让模型提前创建的方法
|
|
|
|
|
//ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
|
2022-01-24 16:12:20 +08:00
|
|
|
|
//Database.SetCommandTimeout(30000);
|
2021-08-23 19:30:52 +08:00
|
|
|
|
}
|
2022-10-31 23:19:33 +08:00
|
|
|
|
|
2022-11-01 13:46:52 +08:00
|
|
|
|
// protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
|
|
|
|
// {
|
|
|
|
|
// base.OnConfiguring(optionsBuilder);
|
|
|
|
|
// optionsBuilder.UseLazyLoadingProxies();
|
|
|
|
|
// }
|
2022-10-31 23:19:33 +08:00
|
|
|
|
|
2022-07-20 22:15:05 +08:00
|
|
|
|
private readonly MethodInfo? _configureGlobalFiltersMethodInfo =
|
2022-11-01 13:46:52 +08:00
|
|
|
|
typeof(DefaultShardingDbContext).GetMethod(nameof(ConfigureGlobalFilters),
|
|
|
|
|
BindingFlags.Instance | BindingFlags.NonPublic);
|
2021-08-23 19:30:52 +08:00
|
|
|
|
|
|
|
|
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
|
|
|
|
{
|
|
|
|
|
base.OnModelCreating(modelBuilder);
|
|
|
|
|
modelBuilder.ApplyConfiguration(new SysUserModMap());
|
|
|
|
|
modelBuilder.ApplyConfiguration(new SysTestMap());
|
|
|
|
|
modelBuilder.ApplyConfiguration(new SysUserLogByMonthMap());
|
2022-08-02 21:30:04 +08:00
|
|
|
|
|
|
|
|
|
modelBuilder.Entity<SysUserLogByMonth>().HasData(new SysUserLogByMonth() { Id = "1", Time = DateTime.Now });
|
|
|
|
|
modelBuilder.Entity<SysTest>().HasData(new SysTest() { Id = "1", UserId = "123" });
|
2021-08-23 19:30:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-11-01 13:46:52 +08:00
|
|
|
|
|
2022-07-20 22:15:05 +08:00
|
|
|
|
protected void ConfigureGlobalFilters<TEntity>(ModelBuilder modelBuilder, IMutableEntityType entityType)
|
|
|
|
|
where TEntity : class
|
|
|
|
|
{
|
|
|
|
|
var filterExpression = CreateFilterExpression<TEntity>();
|
|
|
|
|
|
|
|
|
|
if (filterExpression != null) modelBuilder.Entity<TEntity>().HasQueryFilter(filterExpression);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected Expression<Func<TEntity, bool>>? CreateFilterExpression<TEntity>() where TEntity : class
|
|
|
|
|
{
|
|
|
|
|
Expression<Func<TEntity, bool>>? expression = null;
|
|
|
|
|
if (typeof(TEntity) == typeof(SysTest))
|
2022-11-01 13:46:52 +08:00
|
|
|
|
{
|
2022-07-20 22:15:05 +08:00
|
|
|
|
expression = e => ((IUser)e).UserId == "123";
|
2022-11-01 13:46:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (typeof(TEntity) == typeof(SysUserMod))
|
|
|
|
|
{
|
2022-07-20 22:15:05 +08:00
|
|
|
|
expression = e => ((IAge)e).Age == 99;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return expression;
|
|
|
|
|
}
|
2022-11-01 13:46:52 +08:00
|
|
|
|
|
2021-10-11 20:58:55 +08:00
|
|
|
|
public IRouteTail RouteTail { get; set; }
|
2021-08-23 19:30:52 +08:00
|
|
|
|
}
|
2022-11-01 13:46:52 +08:00
|
|
|
|
}
|