Print Page | Close Window

Peeking into several databases of the same type

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce Classic
Forum Discription: For .NET 2.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=842
Printed Date: 28-Apr-2025 at 5:10am


Topic: Peeking into several databases of the same type
Posted By: jcdk
Subject: Peeking into several databases of the same type
Date Posted: 12-Jun-2008 at 6:39am
Hi,
 
I have a question about the persistence manager: I need to peek into several databases, all with the same schema and all mapped with the same model.dll before I decide wich one to use in my application. I have created my own DataSourceKeyResolver which returns a different connection string based on an entry in the registry. Simplified what I do is this:
 

Dim aEmployee As Employee

Dim EmployeeQuery As New EntityQuery(GetType(Employee))

Registry.SetValue("HKEY_CURRENT_USER\SOFTWARE\DimensionProject", "DataBase", "DP1")

'The line above instructs the datasourcekeyresolver to return a connection string to DP1
 
PersMgr = PersistenceManager.DefaultManager

EmployeeQuery.AddClause(Employee.EmployeeCodeEntityColumn, EntityQueryOp.EQ, "JC")

aEmployee = PersMgr.GetEntity(Of Employee)(EmployeeQuery)

MsgBox(aEmployee.Name)

'This shows data from DP1
 
PersMgr.QueryCache.Clear()

Dim pEntityTables As DataTableCollection = PersMgr.DataSet.Tables

Dim anEntityTable As DataTable

For Each anEntityTable In pEntityTables

ClearEntityTable(anEntityTable)

Next

PersMgr.Clear()

Registry.SetValue("HKEY_CURRENT_USER\SOFTWARE\DimensionProject", "DataBase", "DP2")

'Datasourcekeyresolver to return connection string to DP2. If I break the program in the
'keyresolver it DOES get called when I create the persistencemanager in th next line,
'and it DOES return the correct connectionstring
 
PersMgr = New PersistenceManager(True)

EmployeeQuery.AddClause(Employee.EmployeeCodeEntityColumn, EntityQueryOp.EQ, "JC")

aEmployee = PersMgr.GetEntity(Of Employee)(EmployeeQuery, QueryStrategy.DataSourceOnly)

MsgBox(aEmployee.Name)

'This still shows me data from DP1

If I "quickwatch" the peristencemanager and look at PersMgr.DataSourceResolver.mDataSourceResolver.DataSourceKeys.Items(0) it shown the correct connectionstring to DP2, but I still get data from DP1.
Am I missing a point somewhere or is it not possible to do something like this?
 
Best regards
Jesper Carstensen



Replies:
Posted By: jcdk
Date Posted: 13-Jun-2008 at 1:04am
Hi All
 
I solved it myself by rewriting my code.
 
I created an array of PersistenceManagers, each created with a different DatasourceKeyExtension (equal to the database name). I also modified my DataSourceKeyResolver to return a dfferent connection string for each DataSourceKeyExtension.
 
Now I can connect and disconnect the PersistenceManagers as I need them.
 
Best regards
Jesper Carstensen


Posted By: davidklitzke
Date Posted: 13-Jun-2008 at 7:36am
Jesper.
 
That is the correct technique.  You must have different and unique DataSourceKeyExtensions.  Then the DataSourceKeyResolver should return a different connection string for each DataSourceKeyExtension.



Print Page | Close Window