abp vnext 多租户实现ITenantStore遇到的一个问题
完全按照官方文档实现的TenantStore,但是就是不切换数据库。这什么幺蛾子?后来经过测试发现是ConnectionStrings的key不对。
MD,这个问题之前解决过,只是事过几个月再回来弄,具体细节给忘记了。这次一定要记录下来,免得下次再踩坑。
public async Task<TenantConfiguration> FindAsync(Guid id) { var client = _httpClientFactory.CreateClient(); var response = await client.GetStringAsync($"{_apiHelper.TenantApi}/api/tenant/Get?id=" + id); var jObject = JObject.Parse(response); var data = jObject["data"]; var tenant = JsonConvert.DeserializeObject<Tenant.Tenant>(data.ToString()); if (tenant == null) throw new UserFriendlyException("租户不存在"); var connectionStrings = new ConnectionStrings { { "Equipment", tenant.TenantConnectionStrings.FirstOrDefault().Value } }; TenantConfiguration config = new() { Id = id, Name = tenant.Name, ConnectionStrings = connectionStrings }; return config; }
上面标红的连接字符串名称一定要与Domain项目EquipmentDbProperties.cs中的静态变量ConnectionStringName保持一致
public static class EquipmentDbProperties { public static string DbTablePrefix { get; set; } = "equip_"; public static string DbSchema { get; set; } = null; public const string ConnectionStringName = "Equipment"; }
可以使用如下方式进行替代,避免写死。
var connectionStrings = new ConnectionStrings { { EquipmentDbProperties.ConnectionStringName, tenant.TenantConnectionStrings.FirstOrDefault().Value } };