Issue using multiple DataContext's against the same database

Issue using multiple DataContext's against the same database

Old forum URL: forums.lhotka.net/forums/t/5910.aspx


cmclernon posted on Monday, December 01, 2008

Hi all,

I am fairly new to CSLA and am not sure where to look to see what is causing the following error but believe it's something simple that some of you CSLA guru's could tell me fairly quickly :-)

Unable to cast object of type 'Csla.Data.ContextManager`1[DalLinq.Parts.PartsDataContext]' to type 'Csla.Data.ContextManager`1[DalLinq.Core.CoreDataContext]'.

I have set up multiple DataContexts, two of which are PartsDataContext and CoreDataContext. I get the error above when I am using PartsDataContext and try to call a business object that uses CoreDataContext.  The code I'm using to setup the DataContexts is as follows:

using (var ctx = ContextManager<DalLinq.Core.CoreDataContext>.GetManager(DalLinq.Database.MyDb))

using (var ctx = ContextManager<DalLinq.Parts.PartsDataContext>.GetManager(DalLinq.Database.MyDb))

Does anyone know what is causing this problem?  As I look back at what I've written, would it be anything to do with both Context's pointing at the same database?

Thanks,

Colm

ajj3085 replied on Monday, December 01, 2008

If you look at how the contextmanager is implemented, this makes sense.  Your second call uses the sasme string as the first call, and the internal dictionary uses that string as the key to the dictionary.  So your second call effectively replaces the ContextManager<DalLinq.Core.CoreDataContext> with the ContextManager<DalLinq.Parts.PartsDataContext> instance.  Then, when you try to u se the Core context you get the Parts context... and everything goes to hell.  You also have a connection floating around in memory that isn't properly disposed.. which could also lead to other problems.

cmclernon replied on Tuesday, December 02, 2008

Cheers Andy.

I've changed the code to use a different string name but am still getting the same problem.  Looking at how ContextManager is implemented, it seems that the ContextManager uses the database connection string as the key rather than what this string is called in the application settings which means that you can only use one datacontext concurrently that points to the same database.  I suppose this is in an effort to contain one open connection to the database, most likely to avoid MS DTC kicking in?

ajj3085 replied on Tuesday, December 02, 2008

Are you using the overload that takes a boolean as the second paramter, and passing it the database name, and not the connection string?  That should work the way you want, but yes, it will always cause DTC to try and start.

cmclernon replied on Tuesday, December 02, 2008

Cheers Andy,

No, I'm not using the overload.

I'm refactoring to use only one ConextManager/dbml as that seems to be the way it's supposed to be done.  Don't want the performance hit of DTC either.

Thanks for your replies,

Colm

Copyright (c) Marimer LLC