When configuring the database to use Vector, and you are on EF Core 10, you can have a problem with the instructions if you are using a custom NpgsqlDataSourceBuilder for any reason.
For example, if you are following the current EF Core 10 README instructions that say this:
// Configure the connection
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseNpgsql("connString", o => o.UseVector());
}
and if you configure your database slightly differently in this way:
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
NpgsqlDataSourceBuilder dataSourceBuilder = new(connectionString);
NpgSqlDataSource dataSource = dataSourceBuilder.Build();
optionsBuilder.UseNpgsql(dataSource, o => o.UseVector());
}
Then you are missing this bit of configuration: dataSourceBuilder.UseVector(); before calling dataSource = dataSourceBuilder.Build();. If you leave this out, during runtime, saving a Vector will throw this exception: System.InvalidCastException: Writing values of 'Pgvector.Vector' is not supported. As a new user, this was confusing for me. Luckily, I came across @MoondanceZ's comment but I missed the critical part of his changes the first time I read it.
I recommend doing 1 of 2 of these options:
- Do nothing and let this Issue serve as documentation
- Explicitly change the documentation to use a
NpgsqlDataSourceBuilder this use case is covered, if the user uses a string connection string instead, then it will just work.
In case you want to do option 2, I will open a PR to modify the docs, if not, you can just close the PR.
When configuring the database to use Vector, and you are on EF Core 10, you can have a problem with the instructions if you are using a custom
NpgsqlDataSourceBuilderfor any reason.For example, if you are following the current EF Core 10 README instructions that say this:
and if you configure your database slightly differently in this way:
Then you are missing this bit of configuration:
dataSourceBuilder.UseVector();before callingdataSource = dataSourceBuilder.Build();. If you leave this out, during runtime, saving a Vector will throw this exception:System.InvalidCastException: Writing values of 'Pgvector.Vector' is not supported. As a new user, this was confusing for me. Luckily, I came across @MoondanceZ's comment but I missed the critical part of his changes the first time I read it.I recommend doing 1 of 2 of these options:
NpgsqlDataSourceBuilderthis use case is covered, if the user uses a string connection string instead, then it will just work.In case you want to do option 2, I will open a PR to modify the docs, if not, you can just close the PR.