Ticket B253494
Visible to All Users

How to avoid NullReferenceException in the CreateDefaultObjectSpaceProvider method when DBUpdater tool is used

created 11 years ago (modified 6 years ago)

Problem description
DBUpdater currently supports a single connection string. It parses the target application configuration file looking for a connection string named "connectionString". If the updater is called with the -connectionString parameter, this connection string is used instead (refer to DBUpdater - Provide the capability to pass a connection string as a parameter for more details). This connection string is assigned to the XafApplication.ConnectionString property and is then used to create a default object space provider. If the application uses multiple object space providers and connection strings for them are stored in the configuration file, the XafApplication.CreateDefaultObjectSpaceProvider method usually looks as follows:

C#
protected override void CreateDefaultObjectSpaceProvider(CreateCustomObjectSpaceProviderEventArgs args) { args.ObjectSpaceProviders.Add( new XPObjectSpaceProvider(ConfigurationManager.ConnectionStrings["ConnectionStringXpo"].ConnectionString, null)); args.ObjectSpaceProviders.Add( new EFObjectSpaceProvider(typeof(MyDbContext), ConfigurationManager.ConnectionStrings["ConnectionStringEF"].ConnectionString)); }

This approach is described in the eXpressApp Framework > Task-Based Help > How to: Use Both Entity Framework and XPO in a Single Application topic and this code is automatically generated by the XAF Solution Wizard when both XPO and EF are selected.

In this scenario, DBUpdater cannot initialize the databases because connection strings with other names are not extracted from the application configuration file and the updater throws NullReferenceException on the application setup (see T234726). This happens because when invoked from the DBUpdater context, the ConfigurationManager refers to the updater's config file where no connection strings are declared.

Current solutions

Make sure that you register providers in the CreateDefaultObjectSpaceProvider method or in the CreateCustomObjectSpaceProvider event handled in the application or module class. If the CreateCustomObjectSpaceProvider event is handled outside the application or module instance (e. g., in Program.cs), it won't work because the Program class code is not executed by the updater. Then, use any of the following solutions.
1.  Stop using System.Configuration.ConfigurationManager in the CreateDefaultObjectSpaceProvider method and CreateCustomObjectSpaceProvider event handler. Instead, specify the connection string directly as follows:
new XPObjectSpaceProvider("your connection string for XPO here", null));2. Temporarily modify the DBUpdater\DBUpdater.v14.2.exe.config file by copying the <connectionStrings/> section from the XAF application's configuration file. For example:

XML
<connectionStrings> <add name="ConnectionStringXpo" connectionString="Integrated Security=SSPI;Pooling=false;MultipleActiveResultSets=True;Data Source=.;Initial Catalog=XpoDatabase"/> <add name="ConnectionStringEF" connectionString="Integrated Security=SSPI;Pooling=false;MultipleActiveResultSets=True;Data Source=.;Initial Catalog=EFDatabase"/> </connectionStrings>

3.  Modify the DBUpdater source code (DevExpress.ExpressApp.Tools\DBUpdater\Program.cs) to support multiple connection strings in the manner you specify them in your XAF application and build a custom updater.

Show previous comments (4)
Dennis Garavsky (DevExpress) 11 years ago

    @Dominic:
    It all looks like your DbContext descendant does not have the following member in it:

    C#
    public DbSet<DevExpress.ExpressApp.EF.Updating.ModuleInfo> ModuleInfo { get; set; }

    I hope this helps. Otherwise, please create a separate ticket and attach your project there.

    DG DG
    Dominic Grandmont 11 years ago

      i've added the following line in my context.tt file ( i use model first with entity framework 6):
      public virtual DbSet<DevExpress.ExpressApp.EF.Updating.ModuleInfo> ModuleInfo { get; set; }
      but when i try to log in to the app i have a error which is:
      The entity set for the 'DevExpress.ExpressApp.EF.Updating.ModuleInfo' type is not found.
      here is my log file
      any ideas ?

      Dennis Garavsky (DevExpress) 11 years ago

        @Dominic: I checked your log file, but it is not quite clear why this error occurred. Please create a separate ticket in the Support Center and attach your project there so I can research it locally. Thanks.

        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.