Ticket T965831
Visible to All Users

How to change UnitOfWork's connection string dynamically when using IDataLayer

created 4 years ago (modified 4 years ago)

[DevExpress Support Team: CLONED FROM T419520: How to troubleshoot the "Reentrancy or cross thread operation detected" error]
Hello DevExpress,
switch from

string connectionString = "Server=tcp:…,1433;Initial Catalog…";
dataLayer = XpoDefault.GetDataLayer(defaultConnectionString, AutoCreateOption.DatabaseAndSchema);

to TheadSafeDataLayer described here: https://docs.devexpress.com/XPO/DevExpress.Xpo.ThreadSafeDataLayer

Can I still set/change the connection string of an UnitOfWork?
Before:
string connectionString = "Server=tcp:…,1433;Initial Catalog…";
myUnitOfWorkConnectionString = connectionString;

After:
string connectionString = ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
connectionString = XpoDefault.GetConnectionPoolString(connectionString);
myUnitOfWorkConnectionString = connectionString;

Regards. Walter.

Answers approved by DevExpress Support

created 4 years ago

Hello Walter,

Session or UnitOfWork can be connected to a data store in two ways:

  1. Via its ConnectionString property. This is a legacy approach.
  2. Via a Data Layer or an Object Layer.

If you are using the second way, the database connection for multiple Sessions is managed by a single Data Layer. Thus, you need to change the database connection at the Data Layer level. To do this, create a new Data Layer with new connection settings, create new Session or UnitOfWork instances connected to it and replace old instances where this is required. For example, if your application displays a control bound to XPCollection at this moment, you need to recreate this XPCollection with a new Session and bind controls to it.

Please let me know if you have further questions.

    Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

    Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.