ユーザーごとに 1 つのデータベースがあります。つまり、データベースに対してクエリを実行する前に、接続文字列を変更する必要があります。CodeFluent エンティティを使用すると、実行時に接続文字列を変更できます。
CodeFluentContext context = CodeFluentContext.Get(MyApp.Constants.MyAppStoreName);
CodeFluentPersistence persistence = context.Persistence;
persistence.ConnectionString = GetCurrentTenantConnectionString();
var products = ProductCollection.LoadAll();
または、カスタムを作成できますCodeFluentPersistence
:
public class MultiTenantPersistence : CodeFluentPersistence
{
public MultiTenantPersistence(CodeFluentContext context) : base(context)
{
}
public override string ConnectionString
{
get
{
return GetCurrentTenantConnectionString();
}
set
{
base.ConnectionString = value;
}
}
private string GetCurrentTenantConnectionString()
{
// TODO Implement your own logic
return $"Server=sample;Database=sample_{Context.User.UserName};Trusted_Connection=True;";
}
}
MultiTenantPersistence
次に、構成ファイルにを登録する必要があります。
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="Samples" type="CodeFluent.Runtime.CodeFluentConfigurationSectionHandler, CodeFluent.Runtime" />
</configSections>
<Samples persistenceHookTypeName="Sample.MultiTenantPersistence, Sample" />
</configuration>