Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MultiTenancy多租户,不同DB的方式,netcore里如何注入连接字符串 #261

Closed
zxingdream opened this issue Jul 30, 2021 · 8 comments
Assignees

Comments

@zxingdream
Copy link

如题,
要实现不同租户的数据库切换,通过requeHeader里通过参数获取到要切换的db参数,从而组合连接字符串,
现在的问题是 字符串组合好了,现在不知道如何动态的切换connStr。
请赐教!祝愉快!

@shuxinqin
Copy link
Owner

在创建dbconext的地方呗??

@zxingdream
Copy link
Author

尝试了 但是好像还是有问题 大佬能不能放个netcore的demo呀

@shuxinqin
Copy link
Owner

services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddScoped<Chloe.IDbContext>(a =>
{
    var httpRequest = a.GetService<IHttpContextAccessor>().HttpContext.Request;
    string connectionString = "根据 httpRequest 拼接";
    return new Chloe.SqlServer.MsSqlContext(connectionString);
});

@zxingdream
Copy link
Author

image

@zxingdream
Copy link
Author

这个跟你提供的逻辑是一样的呀 测试没通过耶

@shuxinqin
Copy link
Owner

没通过原因是啥?

@zxingdream
Copy link
Author

解决了 谢谢大佬

@shuxinqin shuxinqin added this to the 多租户(MultiTenancy) milestone Jul 31, 2021
@shuxinqin shuxinqin pinned this issue Jul 31, 2021
@shuxinqin shuxinqin changed the title 多租户,不同DB的方式,netcore里如何注入连接字符串 MultiTenancy多租户,不同DB的方式,netcore里如何注入连接字符串 Jul 31, 2021
@shuxinqin shuxinqin removed this from the 多租户(MultiTenancy) milestone Jul 31, 2021
@shuxinqin shuxinqin self-assigned this Jul 31, 2021
@zxingdream zxingdream reopened this Jul 31, 2021
@MonsterUncle
Copy link

其实大佬可以考虑加个多数据库上下文注入的功能,能够动态切换就可以,可以满足多租户的要求了,然后再解决跨库事务

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants