从多个数据库中获取数据
我有多个数据库,例如 DB1、DB2、DB3 和 MembershipDB。 每个数据库都有名为 ApplicationID 和 GUID 列。以 MembershipID 作为 GUID 的用户集。 每个数据库的每个用户与其他数据库的用户都是隔离的(没有关系)。 会员数据库包含用户 MembershipID 和数据库的 ApplicationID 、密码、用户名、电子邮件等。
我需要创建一个通用登录系统,例如当用户提供登录凭据时,将从 MembershipDB 进行身份验证并获取他的数据从他注册的数据库中。
例如:来自 DB1 的用户提供了他的登录详细信息,身份验证是从membershipDB 完成的。现在,他的其余数据将从 DB1(因为他已注册到 DB1)获取到他的仪表板。我有点困惑如何构建这个登录系统。
认证部分没有问题。但如何连接到正确的数据库来为用户获取正确的数据。
欢迎大家提出宝贵的意见和建议。
我正在使用 Microsoft SQL SERVER 2008、Entity Framework 4.2(数据库 ist 方法)、WCF、asp.net MVC 4
I have multiple databases say DB1, DB2, DB3 and MembershipDB.
Every Database has GUID column named ApplicationID & set of Users having MembershipID as GUID.
Each User of every Database is Isolated (have no relationship) with User of other Database.
Membership Database contains the Users MembershipID and Database’s ApplicationID , password, username, email etc.
I need to create a common login system like When a user provides his login credential authentication is to be done from MembershipDB and his data is to be fetched from the database he is registered to.
Eg: a user from DB1 provides his login details, authentication is done from membershipDB. Now rest of his data is to be fetched from DB1(as he is registered to DB1) to his dashboard. I am bit confuse how to build this login system.
Authentication part is no problem. But how to connect to the correct DB to get the correct data for the user.
All the valuable comments and suggestions are welcome.
I am using Microsoft SQL SERVER 2008, Entity Framework 4.2 (database ist approach), WCF, asp.net MVC 4
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在会员数据库中,您应该有一列记录此使用将转到哪个数据库
例如:
id, username,password, applicationid, email, DBName
//如果数据库在不同的服务器上,可以连接到服务器2并分配给DBobj2
//然后使用与上面类似的方法
in member ship DB, you should have a column that record which database this use will go to
for example:
id, username,password, applicationid, email, DBName
//if the Database are in different servers, you can connect to server 2 and assign to DBobj2
//Then use the similar method as above
如果我理解正确,那么您需要动态连接到不同的数据库,而无需修改代码。
假设您有 2 个客户 Customer1 和 Customer2。您为这些客户创建了 2 个名为 Database_Customer1 和 Database_Customer2 的数据库。
将实体框架与任何名为“CustomerDbSpecificEntities”的客户数据库进行映射。
现在您需要在 .config 文件中添加以下连接字符串。
在数据访问层的代码中,
在公共数据库(会员数据库)中创建一个表(CustomerDbConnection)来存储客户名称及其数据库连接字符串名称的映射。
“GetConnectionStringNameForCustomer(CustomerName)”在 CustomerDbConnection 表中搜索并返回给定客户的连接字符串名称。用于建立与客户特定数据库的连接。
现在,如果您将来有新客户,那么您必须做出以下更改,
1. 在 CustomerDbConnection 中添加新行。
2. 在.config 文件中添加新的连接字符串,
如果您需要更多详细信息,请告诉我。
If I understood you correctly then you need to connect to different databases dynamically without modifying your code.
Lets assume you have 2 customers Customer1 and Customer2. You created 2 databases with names Database_Customer1 and Database_Customer2 for these customers.
Map entity framework with any of the Customer's database with name 'CustomerDbSpecificEntities'.
Now you need to add following connection strings in your .config file.
In your code in Data Access Layer,
Create a table (CustomerDbConnection) in common database (membership database) to store mapping of Customer Name and its database connection string name.
'GetConnectionStringNameForCustomer(CustomerName)' searches in CustomerDbConnection table and returns the connection string name for given customer. Which is used to established the connection to customer specific database.
Now if you have new customer in future then you have to make following changes,
1. Add new row in CustomerDbConnection.
2. Add new connection string in .config file
Please let me know if you need more detailed about it.