Closed jerome-zenfolio closed 9 months ago
@jerome-zenfolio it would be really helpful if you could put together a simple, runnable repro.
@roji, I wrote a simpler code, but I cannot reproduce the issue. In order to reproduce, I had to comment-out UseVector
initialization. So, there must be some bug with the way I am initializing it.
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.Extensions.DependencyInjection;
using Pgvector;
namespace VectorDemo
{
class Program
{
static void Main()
{
string connectionString = "Host=localhost;Database=dev_zenfolio_product_catalog;Port=5432;User Id=postgres;Password=postgres;";
var serviceProvider = new ServiceCollection()
.AddDbContextPool<MyDbContext>(options =>
options.UseNpgsql(connectionString, builder => builder.UseVector()))
.BuildServiceProvider();
using var scope = serviceProvider.CreateScope();
var dbContext = scope.ServiceProvider.GetRequiredService<MyDbContext>();
var databaseCreator = dbContext.Database.GetService<IDatabaseCreator>() as IRelationalDatabaseCreator;
databaseCreator?.CreateTables();
var newRecord = new MyEntity { Name = "Sample", Embedding = new Vector(new[] { 1f, 1, 1 }) };
var dbContext2 = scope.ServiceProvider.GetRequiredService<MyDbContext>();
dbContext2.MyEntities.Add(newRecord);
dbContext2.SaveChanges();
foreach (var entity in dbContext.MyEntities)
{
Console.WriteLine($"ID: {entity.Id}, Name: {entity.Name}");
}
}
}
public class MyEntity
{
public int Id { get; set; }
public string? Name { get; set; }
public Vector? Embedding { get; set; }
}
public class MyDbContext : DbContext
{
public MyDbContext(DbContextOptions<MyDbContext> options) : base(options)
{
}
public DbSet<MyEntity> MyEntities { get; set; }
}
}
I'll close out issue, if I can't reproduce. Thanks!
Okay there is some progress; I've found an issue in my original code. I had to update the DI routine like this:
serviceCollection.AddDbContextPool<CatalogDbContext>(options =>
{
NpgsqlDataSource dataSource = ...
options.UseVector();
options.UseNpgsql(dataSource, pgOptions => pgOptions.UseVector());
});
It looks the call UseVector is required on both NpgSqlDataSourceBuilder.UseVector
and NpgSqlDbContextOptionsBuilder
. I didn't invoke the former. Now that fixed the issue with inserting new row or reading vectors from an existing table. Now on to hunt why it fails to create a table with a vector column when I run ef migration from command line.
Closing this for now, if I can create a reproducible sample, will create a new one.
Hello :wave:
I have a .NET 8 project and I was able to duplicate the code and run this test without any issues.
However, when I attempt to use DbContextPool, I cannot get this to work. Sorry, this is not a reproducible code but a copy-paste from a larger codebase.
This is my DbContext:
Dependency injection:
When I run the EF Migration, I am getting the error:
If I create the table manually and attempt to insert:
I am getting the following error:
If I manually insert rows and attempt to read:
I am getting:
Could you please point me what I could be doing wrong. Thank you!
Jerome