Ticket T988926
Visible to All Users

How to set AutoCreateOption dynamycally

created 4 years ago (modified 4 years ago)

[DevExpress Support Team: CLONED FROM K18098: How to create a custom XPO connection provider and then use it in an XAF application]

I have implemented this solution for a Postgres connection provider but I get an exception when the application starts and the database is not existing because the CreateProviderFromString autoCreateOption parameter is set to SchemaAlreadyExists.
Forcing it to DatabaseAndSchema solves the problem but I would like to be able to change it from outside, i.e. the caller should pass a different parameter.

C#
using DevExpress.Xpo.DB; using Npgsql; using System; using System.Data; namespace PrgDig.PostGIS { /// <summary> /// <see cref="https://supportcenter.devexpress.com/ticket/details/k18098/how-to-create-a-custom-xpo-connection-provider-and-then-use-it-in-an-xaf-application"/> /// </summary> public class PostGISConnectionProvider : PostgreSqlConnectionProvider { public PostGISConnectionProvider(IDbConnection connection, AutoCreateOption autoCreateOption) : base(connection, autoCreateOption) { } public new static IDataStore CreateProviderFromString(string connectionString, AutoCreateOption autoCreateOption, out IDisposable[] objectsToDisposeOnDisconnect) { IDbConnection connection = new NpgsqlConnection(connectionString); objectsToDisposeOnDisconnect = new IDisposable[] { connection }; return CreateProviderFromConnection(connection, autoCreateOption); } public new static IDataStore CreateProviderFromConnection(IDbConnection connection, AutoCreateOption autoCreateOption) { return new PostGISConnectionProvider(connection, autoCreateOption); } public new static string GetConnectionString(string server, int port, string userId, string password, string database) { return $"{XpoProviderTypeParameterName}={XpoProviderTypeString};Server={server};Port={port};Database={database};User ID={userId};Password={password}"; } public new static string GetConnectionString(string server, string userId, string password, string database) { return $"{XpoProviderTypeParameterName}={XpoProviderTypeString};Server={server};Database={database};User ID={userId};Password={password}"; } public new static void Register() { RegisterDataStoreProvider(XpoProviderTypeString, CreateProviderFromString); RegisterDataStoreProvider(typeof(PostgreSqlConnectionProvider).FullName, CreateProviderFromConnection); } public new const string XpoProviderTypeString = "PostGIS"; /// <summary> /// <see cref=">https://supportcenter.devexpress.com/ticket/details/t439479/add-support-for-geometry-in-x"/> /// <see cref="https://community.devexpress.com/blogs/oliver/archive/2017/01/16/xpo-sql-server-and-spatial-data-revisited.aspx"/> /// </summary> /// <param name="value"></param> /// <param name="args"></param> /// <returns></returns> protected override object ReformatReadValue(object value, ReformatReadValueArgs args) { if (value != null && value is NetTopologySuite.Geometries.Geometry) { return value; } else { return base.ReformatReadValue(value, args); } } } }
Show previous comments (4)
Anatol (DevExpress) 4 years ago

    Hello Massimo,

    From your comment it is not quite clear whether any issue remains after you skip startup exceptions. These exceptions are expected. XAF creates a connection provider with AutoCreateOption = DatabaseAndSchema only when it detects that a schema update is required. During this detection, handled UnableToOpenDatabaseException and SchemaCorrectionNeededExceptions may occur. You won't see them with the default Connection Provider if the Debugging -> Options -> Enable Just My Code option is enabled in your Visual Studio. With a custom Connection Provider, these exceptions occur in your code, and the debugger shows them.

    M M
    Massimo Endrighi Geopartner 4 years ago

      I confirm that skipping the exception solves definitively the problem, but the exception I get is not the one you posted in your first post but is
      UnableToOpenDatabaseException.

      Note that an application will throw SchemaCorrectionNeededExceptions in this case, but they are handled in our code. If you see them in the debugger, just skip them with F5. Please let me know if this works for you.

      Thank you for support

      Anatol (DevExpress) 4 years ago

        Hello Massimo,

        Thank you for the confirmation.
        Just for your information, XPO throws SchemaCorrectionNeededException when it cannot find a column or a table and UnableToOpenDatabaseException when it cannot find a database.

        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.