24 lines
970 B
C#
24 lines
970 B
C#
namespace ConsoleApp2.Options;
|
|
|
|
public class TenantDbOptions
|
|
{
|
|
public string? TenantKey { get; set; }
|
|
|
|
/// <summary>
|
|
/// Key-Value: {DbName}-{TenantKeyLessThan}
|
|
/// </summary>
|
|
public Dictionary<string, int>? DbList { get; set; }
|
|
|
|
public string GetDbNameByTenantKeyValue(int tenantKeyValue)
|
|
{
|
|
// var dictionary = new SortedDictionary<int, string>();
|
|
// DbList.ForEach(pair => dictionary.Add(pair.Value, pair.Key));
|
|
// 注意配置顺序
|
|
if(DbList is null) throw new ApplicationException("分库配置中没有发现任何数据库");
|
|
var dbName = DbList.Cast<KeyValuePair<string, int>?>()
|
|
.FirstOrDefault(pair => pair?.Value != null && pair.Value.Value > tenantKeyValue)!.Value.Key;
|
|
return dbName ??
|
|
throw new ArgumentOutOfRangeException(nameof(tenantKeyValue),
|
|
$"分库配置中没有任何符合'{nameof(tenantKeyValue)}'值的数据库");
|
|
}
|
|
} |