简体   繁体   English

多数据库连接字符串:将其存储在我的应用程序中的位置

[英]Multi-database connection string: where store it in my application

I have different database with same schema. 我有具有相同架构的不同数据库。 I need to connect to one of them according to the client that has been connected. 我需要根据已连接的客户端连接到其中之一。 For example if ClientA is connected i must connect to DatabaseA; 例如,如果ClientA已连接,则我必须连接到DatabaseA; If ClientB is connected i must connect to DatabaseB; 如果ClientB已连接,则必须连接到DatabaseB; Now I have a multi-layered application: a Presentation Layer, a Service Layer and a repository Layer. 现在,我有一个多层应用程序:一个表示层,一个服务层和一个存储库层。 With some logic I obtain the correct connection string! 通过某种逻辑,我可以获得正确的连接字符串!

So.. What I need now. 所以..我现在需要的。

I don't know where to store the connection string for that client! 我不知道该客户端的连接字符串存储在哪里! I was thinking in Session. 我在开会时在想。 But if I store it in the session I have to pass it in each call I do to the repository layer, because Session is not visible at repository. 但是,如果我将其存储在会话中,则必须在每次调用时将其传递给存储库层,因为在存储库中看不到会话。 I think this is the worst solution I could take. 我认为这是我可以采取的最糟糕的解决方案。

I would like to have the selected connection string just a repository layer in a global variable for that client. 我希望所选的连接字符串只是该客户端的全局变量中的存储库层。

Suggestion? 建议? Thank you 谢谢

如果要保留网站的全局数据-可以在Application类中存储任何数据,则可以通过以下链接检查更多详细信息: http : //www.asp.net/web-forms/overview/data-访问/缓存数据/缓存数据在应用程序启动cs中如果您有应用程序的任何单独部分,则可以将共享数据保留在xml文件中,并在每个应用程序域中创建ConfigManager,或者可以使用数据库表。

With so many discussions already done, I would like to share my thoughts based on my experience. 在进行了如此多的讨论之后,我想根据我的经验分享我的想法。

It is always advisable to get the connection string resolved at the repository layer for the client that is connecting to. 始终建议在存储库层为要连接的客户端解析连接字符串。 This can be however cached within the application. 但是,这可以缓存在应用程序中。

You will be having your business layer that contains all of your business entities that reside in the application and it will be accessible throughout all the layers of the application. 您将拥有包含位于应用程序中的所有业务实体的业务层,并且可以在应用程序的所有层中对其进行访问。

If the connection string resolution mechanism reside in this layer [of course cached], it will be a elegant design and easy to use. 如果连接字符串解析机制位于此层中[当然是已缓存的],则它将是一个优雅的设计并且易于使用。

Probably the best way for you is to have a single database where user-related tables have foreign keys to Users table. 可能对您来说最好的方法是拥有一个数据库,其中与用户相关的表具有与Users表的外键。

You can learn more about multi-tenant architectures from this Microsoft article. 您可以从这篇 Microsoft文章中了解有关多租户体系结构的更多信息。 Here is a whole topic about this subject: asp.net mvc multiple connection strings 这是有关此主题的整个主题: asp.net mvc多个连接字符串

Why aren't you creating a ConnectionString repository in your app, and based on the logic you get the correct CS. 为什么不在应用程序中创建ConnectionString存储库,并根据逻辑获得正确的CS。 Something like a lookup table, static. 诸如查找表之类的东西,是静态的。 In case you'd like to modify the ConnectionStrings, you could use your app.config and get them from there anytime you need them. 如果您想修改ConnectionStrings,可以使用app.config并在需要时从那里获取它们。

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

相关问题 在多数据库环境中映射正确的连接字符串,并在运行时一次以登录用户的会话状态维护成员身份 - Mapping correct connection string in multi-database environment & maintaining membership with session state of logged in user once at runtime 根据在多数据库MVC应用程序中选择的数据库选择特定的DBContext - Choosing specific DBContext according to database selected in multi-database MVC application 将连接字符串存储在数据库中,并在应用程序启动时检索它 - Store connection string in database and retrieve it on application startup 具有实体框架的动态多数据库上下文 - Dynamic multi-database Context with Entity framework 存储连接字符串的位置 - Where to store Connection String C#:合并多数据库驱动程序接口 - C#: Merging multi-database driver interface 与数据库应用程序的连接字符串 - Connection string to Database Application EntityFramework在哪里获得到本地数据库的连接字符串? - Where does EntityFramework get the connection string to my local database? 找不到我的数据库文件和连接字符串所在的位置? - Unable to find where my database file and connection string is located? 在桌面应用程序中存储类库的连接字符串的位置? 我可以在app.config中使用吗? - Where to store connection string for class library in desktop application? Can I use in app.config?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM