2015-11-24 26 views
2

我有一个使用CodeFluent构建的应用程序,该应用程序作为SAAS解决方案托管。它使用Ms Azure数据库作为存储,但现在所有客户都在同一个数据库中。考虑到SAAS解决方案的最佳实践,分离数据库会更好。备份/恢复单独的客户端数据会更容易,并且从安全角度来看也更好。我们想使用Azure弹性数据库池。Saas解决方案中的Codefluent独立数据库

然而,这并不简单。 Codefluent使用固定的数据库连接,在web.config中设置。如果我能以某种方式改变它,我怎样才能确定使用哪个数据库。有没有一个会话或httpcontext ...有没有人有同样的挑战,你是如何解决它?

回答

3

每个用户都有一个数据库。这意味着您需要在查询数据库之前更改连接字符串。随着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>