Skip to content

NpgsqlOptionsExtension loses ParameterizedCollectionMode between clones #3795

@pinkfloydx33

Description

@pinkfloydx33

When calling any of the WithXyz methods on NpgsqlOptionsExtension (or corresponding methods on DbContextOptionsBuilder), the Clone method and copy-constructor lose any previous values set for ParameterizedCollectionMode.

public NpgsqlOptionsExtension(NpgsqlOptionsExtension copyFrom)
: base(copyFrom)
{
DataSource = copyFrom.DataSource;
DataSourceBuilderAction = copyFrom.DataSourceBuilderAction;
AdminDatabase = copyFrom.AdminDatabase;
_postgresVersion = copyFrom._postgresVersion;
UseRedshift = copyFrom.UseRedshift;
_userRangeDefinitions = [..copyFrom._userRangeDefinitions];
_enumDefinitions = [..copyFrom._enumDefinitions];
ProvideClientCertificatesCallback = copyFrom.ProvideClientCertificatesCallback;
RemoteCertificateValidationCallback = copyFrom.RemoteCertificateValidationCallback;
ProvidePasswordCallback = copyFrom.ProvidePasswordCallback;
ReverseNullOrdering = copyFrom.ReverseNullOrdering;
}

The base class' _parameterizedCollectionMode field is copied by the base class' copy-constructor, but the "overridden" field is not copied by NpgsqlOptionsExtension. Thus unless you call DbContextOptionsBuilder.UseParameterizedCollectionMode last you will lose the value of the field and it will always revert back to Parameter

options.UseNpgsql("connection", s => { 
    s.UseParameterizedCollectionMode(ParameterTranslationMode.MultipleParameters);
    s.UsePostgresVersion(new Version(17,0));
});

var mode = options.Options.GetExtension<NpgsqlOptionsExtension>().ParameterizedCollectionMode; // ParameterTranslationMode.Parameter

I discovered this while writing tests against a custom IDbContextOptionsConfiguration<> that would set the mode from special configuration. I was losing the value because I was calling other builder methods after the fact. Currently we only leverage Mode=Parameter so it's not a blocking issue or anything, but I suspect the behavior is unintended

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions