[英]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.