コード例 #1
0
    public static IServiceCollection AddPostgreSQLBasedOnEF(this IServiceCollection serviceCollection, Action <PostgresConnectionOptions> action)
    {
        var connectionOptions = new PostgresConnectionOptions();

        action.Invoke(connectionOptions);

        serviceCollection.AddDbContext <PostgresDatabaseContext>(options => options
                                                                 .UseNpgsql(connectionOptions.ConnectionString)
                                                                 .UseQueryTrackingBehavior(
                                                                     connectionOptions.UseTracking ? QueryTrackingBehavior.TrackAll : QueryTrackingBehavior.NoTracking
                                                                     )
                                                                 );

        return(serviceCollection);
    }
コード例 #2
0
        protected GeneralContext(PostgresConnectionOptions connectionOptions)
        {
            DatabaseName  = connectionOptions.DatabaseName;
            DatabaseOwner = connectionOptions.DatabaseOwner;

            PostgresConnectionString = $"server={connectionOptions.Server};" +
                                       $"userId={connectionOptions.UserId};" +
                                       $"password={connectionOptions.Password};" +
                                       $"Pooling={connectionOptions.Pooling};";

            if (string.IsNullOrEmpty(connectionOptions.SslMode) == false)
            {
                PostgresConnectionString += $"Sslmode={connectionOptions.SslMode};";
            }

            if (connectionOptions.TrustServerCertificate.HasValue)
            {
                PostgresConnectionString += $"Trust Server Certificate={connectionOptions.TrustServerCertificate};";
            }

            ConnectionString            = PostgresConnectionString + $"Database={connectionOptions.DatabaseName};";
            MaintenanceConnectionString = PostgresConnectionString + $"Database={connectionOptions.MaintenanceDatabase};";
        }