简体   繁体   English

C#.NET实体框架多租户最佳实践

[英]C# .NET Entity Framework multi-tenant best practice

Consider the following code segment: 考虑以下代码段:

public class DatabaseContext : DbContext
{
   public DatabaseContext(String connectionString) : base(connectionString)
   {            
   }
}

public class ContextNameDatabaseContext : DatabaseContext
{
   public ContextNameDatabaseContext(String connectionString) : base(connectionString)
   {
   }
}

Would one say it is best practice when building the back-end for a multi-tenant solution where each client has its own database and maintain the data state until a user logs out / off? 在构建多租户解决方案的后端时,每个客户都有自己的数据库并维护数据状态,直到用户注销/注销时,有人会说这是最佳实践吗?

Developer using these classes in this instance will need to be aware and careful as to when and how the classes are being used where the 'DatabaseContext' class acts as a base to the 'ContextNameDatabaseContext' class. 在这种情况下使用这些类的开发人员将需要了解和注意何时以及如何使用这些类,其中'DatabaseContext'类充当'ContextNameDatabaseContext'类的基础。

Please advise on any thoughts or suggestions. 请提出任何想法或建议。

One approach is to keep all the database connection strings as parameters in the database. 一种方法是将所有数据库连接字符串保留为数据库中的参数。 However you have to assure that its encrypted. 但是,您必须确保对其进行加密。

Then at your DB layer you can pass the connection as parameter in plain text after decrypting and constructing your connection string accordingly: 然后,在您的数据库层,您可以相应地解密并构造连接字符串后,以纯文本形式将连接作为参数传递:

public class MyDatabase: DbContext
{
    public MyDatabase(string connString)
    {
        this.Database.Connection.ConnectionString = connString;
    }
    public DbSet<Order> Orders{ get; set; }
}

You can also use IOptions if you are using .NET Core to inject the connection string as a dependency. 如果使用.NET Core将连接字符串作为依赖项注入,则也可以使用IOptions

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Azure 数据库水平分片是多租户 C# asp.net 应用程序的最佳解决方案 - Azure Database Horizontal Sharding the best solution for Multi-tenant C# asp.net application Microsoft Bot Framework多租户凭据C# - Microsoft Bot Framework Multi-Tenant Credentials C# 多租户隔离数据库的DAL和配置的最佳实践 - best practice for DAL and configuration on Multi-Tenant isolated DB 实体框架6数据库迁移,用于隔离的多租户设置 - Entity Framework 6 database migrations for isolated multi-tenant setup 具有多个数据库和泛型的实体框架多租户架构 - Entity Framework Multi-Tenant Architecture With Multiple Databases & Generics 多租户实体过滤 - Multi-tenant entity filtration c# .net 核心 MVC 为每个客户定制文本和本地化多租户 - c# .net Core MVC customize text per customer and localization multi-tenant 如何在.Net Core中的应用程序class上使用依赖注入来实现实体框架池化上下文以实现多租户? - How to use Dependency Injection on a application class in .Net Core for an Entity Framework Pooled Context to achieve multi-tenant implementation? C#-在多租户应用中验证颁发者 - C# - Verifying Issuer in a multi-tenant app 多租户ASP .NET应用程序中的隔离 - Isolation in a Multi-tenant ASP .NET Application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM