在上一篇文章中,咱们介绍了如何根据不一样的租户进行数据分离,分离的办法是一个租户一个数据库。html
也提到了这种模式仍是相对比较重,因此本文会介绍一种更加广泛使用的办法: 按表分离租户。git
在目前的to B的系统中,其实每每会有一个Master数据库,里面使用的是系统中主要的数据,各个租户的数据,每每只是对应的订单、配置、客户信息。github
这就形成了,租户的数据不会有不少的种类,他的数据表的数量相对Master来讲仍是比较少的。sql
因此在单一租户数据量没有十分庞大的时候,就没有必要对单一租户数据独立到单一数据库。多个租户数据共享使用一个数库是一个折中的选择。数据库
下图就是对应的数据表结构,其中store1和store2使用不一样的数据表,但有同一个表名后缀和相同结构。缓存
本文的项目仍是沿用上一篇文章的代码,进行加以修改。因此项目中的依赖项仍是那些。
架构
但因为代码中有不少命名很差的地方我进行了修改。而且,因为代码结构太简单,对这个示例实现起来很差,进行了少许的结构优化。ide
1. ModelCacheKeyFactory,这个是EF core提供的对象,主要是要来产生ModelCacheKey函数
2. ModelCacheKey, 这个跟ModelCacheKeyFactory是一对的,若是须要自定义的话通常要同时实现他们俩post
3. ConnectionResolverOption,这个是项目自定义的对象,用于配置。由于咱们项目中如今须要同时支持多种租户数据分离的方式
1. 添加 ITenantDbContext 接口,它的做用是要来规定StoreDbContext中,必须能够返回TenantInfo。
1 public interface ITenantDbContext 2 { 3 TenantInfo TenantInfo{get;} 4 }
咱们同时也须要修改StoreDbContext去实现 ITenantDbContext 接口,而且在构造函数上添加TenantInfo的注入
其中Products已经不是原来简单的一个Property,这里使用DbSet来获取对应的对象,由于表对象仍是使用只读Property会好点。
新增一个方法的重写OnModelCreating,这个方法的主要规定EF core 的表实体(本文是Product)怎么跟数据库匹配的,简单来讲就是配置。
能够看到表名的规则是TenantInfo.Name+"_Products"
1 public class StoreDbContext : DbContext,ITenantDbContext 2 { 3 public DbSet<Product> Products => this.Set<Product>(); 4 5 public TenantInfo TenantInfo => tenantInfo; 6 7 private readonly TenantInfo tenantInfo; 8 9 public StoreDbContext(DbContextOptions options, TenantInfo tenantInfo) : base(options) 10 { 11 this.tenantInfo = tenantInfo; 12 } 13 14 protected override void OnModelCreating(ModelBuilder modelBuilder) 15 { 16 modelBuilder.Entity<Product>().ToTable(this.tenantInfo.Name + "_Products"); 17 } 18 }
2. 建立 TenantModelCacheKeyFactory 和 TenantModelCacheKey
TenantModelCacheKeyFactory的做用主要是建立TenantModelCacheKey实例。TenantModelCacheKey的做用是做为一个键值,标识dbContext中的OnModelCreating否须要调用。
为何这样作呢?由于ef core为了优化效率,避免在dbContext每次实例化的时候,都须要从新构建数据实体模型。
在默认状况下,OnModelCreating只会调用一次就会存在缓存。但因为咱们建立了TenantModelCacheKey,使得咱们有机会判断在什么状况下须要从新调用OnModelCreating
这里是本文中最关键的改动
1 using System; 2 using Microsoft.EntityFrameworkCore; 3 using Microsoft.EntityFrameworkCore.Infrastructure; 4 5 namespace kiwiho.Course.MultipleTenancy.EFcore.Api.Infrastructure 6 { 7 internal sealed class TenantModelCacheKeyFactory<TContext> : ModelCacheKeyFactory 8 where TContext : DbContext, ITenantDbContext 9 { 10 11 public override object Create(DbContext context) 12 { 13 var dbContext = context as TContext; 14 return new TenantModelCacheKey<TContext>(dbContext, dbContext?.TenantInfo?.Name ?? "no_tenant_identifier"); 15 } 16 17 public TenantModelCacheKeyFactory(ModelCacheKeyFactoryDependencies dependencies) : base(dependencies) 18 { 19 } 20 } 21 22 internal sealed class TenantModelCacheKey<TContext> : ModelCacheKey 23 where TContext : DbContext, ITenantDbContext 24 { 25 private readonly TContext context; 26 private readonly string identifier; 27 public TenantModelCacheKey(TContext context, string identifier) : base(context) 28 { 29 this.context = context; 30 this.identifier = identifier; 31 } 32 33 protected override bool Equals(ModelCacheKey other) 34 { 35 return base.Equals(other) && (other as TenantModelCacheKey<TContext>)?.identifier == identifier; 36 } 37 38 public override int GetHashCode() 39 { 40 var hashCode = base.GetHashCode(); 41 if (identifier != null) 42 { 43 hashCode ^= identifier.GetHashCode(); 44 } 45 46 return hashCode; 47 } 48 } 49 }
3. 添加 ConnectionResolverOption 类和 ConnectionResolverType 枚举。
1 using System; 2 3 namespace kiwiho.Course.MultipleTenancy.EFcore.Api.Infrastructure 4 { 5 public class ConnectionResolverOption 6 { 7 public string Key { get; set; } = "default"; 8 9 public ConnectionResolverType Type { get; set; } 10 11 public string ConnectinStringName { get; set; } 12 } 13 14 public enum ConnectionResolverType 15 { 16 Default = 0, 17 ByDatabase = 1, 18 ByTabel = 2 19 } 20 }
4. 调整 MultipleTenancyExtension 的代码结构,而且添加2个扩展函数用于对配置相关的注入。
下面贴出修改事后最主要的3个方法
1 internal static IServiceCollection AddDatabase<TDbContext>(this IServiceCollection services, 2 ConnectionResolverOption option) 3 where TDbContext : DbContext, ITenantDbContext 4 { 5 services.AddSingleton(option); 6 7 services.AddScoped<TenantInfo>(); 8 services.AddScoped<ISqlConnectionResolver, TenantSqlConnectionResolver>(); 9 services.AddDbContext<TDbContext>((serviceProvider, options) => 10 { 11 var resolver = serviceProvider.GetRequiredService<ISqlConnectionResolver>(); 12 13 var dbOptionBuilder = options.UseMySql(resolver.GetConnection()); 14 if (option.Type == ConnectionResolverType.ByTabel) 15 { 16 dbOptionBuilder.ReplaceService<IModelCacheKeyFactory, TenantModelCacheKeyFactory<TDbContext>>(); 17 } 18 }); 19 20 return services; 21 } 22 23 public static IServiceCollection AddTenantDatabasePerTable<TDbContext>(this IServiceCollection services, 24 string connectionStringName, string key = "default") 25 where TDbContext : DbContext, ITenantDbContext 26 { 27 var option = new ConnectionResolverOption() 28 { 29 Key = key, 30 Type = ConnectionResolverType.ByTabel, 31 ConnectinStringName = connectionStringName 32 }; 33 34 return services.AddTenantDatabasePerTable<TDbContext>(option); 35 } 36 37 public static IServiceCollection AddTenantDatabasePerTable<TDbContext>(this IServiceCollection services, 38 ConnectionResolverOption option) 39 where TDbContext : DbContext, ITenantDbContext 40 { 41 if (option == null) 42 { 43 option = new ConnectionResolverOption() 44 { 45 Key = "default", 46 Type = ConnectionResolverType.ByTabel, 47 ConnectinStringName = "default" 48 }; 49 } 50 51 52 return services.AddDatabase<TDbContext>(option); 53 }
其中有一个关键的配置, 须要把上文提到的 TenantModelCacheKeyFactory 配置到dbOptionBuilder
1 if (option.Type == ConnectionResolverType.ByTabel) 2 { 3 dbOptionBuilder.ReplaceService<IModelCacheKeyFactory,TenantModelCacheKeyFactory<TDbContext>>(); 4 }
5. 在 TenantSqlConnectionResolver 的GetConnection方法中修改逻辑,让它同时支持按表分离数据和前文的按数据库分离数据
这个类的名字已经改了,前文的命名不合适。 方法中用到的 option 是 ConnectionResolverOption 类型,须要加到构造函数。
1 public string GetConnection() 2 { 3 string connectionString = null; 4 switch (this.option.Type) 5 { 6 case ConnectionResolverType.ByDatabase: 7 connectionString = configuration.GetConnectionString(this.tenantInfo.Name); 8 break; 9 case ConnectionResolverType.ByTabel: 10 connectionString = configuration.GetConnectionString(this.option.ConnectinStringName); 11 break; 12 } 13 14 if (string.IsNullOrEmpty(connectionString)) 15 { 16 throw new NullReferenceException("can not find the connection"); 17 } 18 return connectionString; 19 }
在本文中,并无使用Code First配置数据库。因此数据库和数据表须要自行建立。
这样作其实更加贴合项目实际,由于具备这种软件架构的项目,每每须要在新增租户的时候进行自动化处理,广泛作法是准备好一批sql,在新增租户的时候自动在对应的数据库中建立一批表
可能会有人提出疑问,以为ef core提供的Migration是具备一样的做用的。这个的确是,可是咱们这里的表是动态的,ef core生成的Migration plan实际上是须要作手动修改的。
Migration 的修改和自定义话是一个大话题,这个须要开另外的文章谈
关于本示例的ef core Migration 实操,请参阅个人另外一篇文章
EF core (code first) 经过自定义 Migration History 实现多租户使用同一数据库时更新数据库结构
1 CREATE TABLE `store1_Products` ( 2 `Id` int(11) NOT NULL AUTO_INCREMENT, 3 `Name` varchar(50) CHARACTER SET utf8mb4 NOT NULL, 4 `Category` varchar(50) CHARACTER SET utf8mb4 DEFAULT NULL, 5 `Price` double DEFAULT NULL, 6 PRIMARY KEY (`Id`) 7 ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1; 8 9 CREATE TABLE `store2_Products` ( 10 `Id` int(11) NOT NULL AUTO_INCREMENT, 11 `Name` varchar(50) CHARACTER SET utf8mb4 NOT NULL, 12 `Category` varchar(50) CHARACTER SET utf8mb4 DEFAULT NULL, 13 `Price` double DEFAULT NULL, 14 PRIMARY KEY (`Id`) 15 ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;
咱们仍是跟前文同样,分别使用store1和store2仲添加一些数据。
调动查询全部product接口
store1:
store2:
这个示例已经完成了。跟前文同样,是一个实操类型的文章。
下一次咱们谈谈怎么根据Schema分离数据。可是Mysql是没有Schema这个概念的,因此咱们须要把SqlServer集成进来
但这样把项目的复杂性又提升的。因此这一次必须把代码抽象好了。
代码已经传上github,请查看part2的分支或查看commit tag是part2的代码内容。
https://github.com/woailibain/EFCore.MultipleTenancyDemo/tree/part2