forked from pgvector/pgvector-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVectorDbContextOptionsExtension.cs
46 lines (33 loc) · 1.59 KB
/
VectorDbContextOptionsExtension.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Query;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.Extensions.DependencyInjection;
namespace Pgvector.EntityFrameworkCore;
public class VectorDbContextOptionsExtension : IDbContextOptionsExtension
{
private DbContextOptionsExtensionInfo? _info;
public virtual DbContextOptionsExtensionInfo Info => _info ??= new ExtensionInfo(this);
public void ApplyServices(IServiceCollection services)
{
new EntityFrameworkRelationalServicesBuilder(services)
.TryAdd<IMethodCallTranslatorPlugin, VectorDbFunctionsTranslatorPlugin>();
services.AddSingleton<IRelationalTypeMappingSourcePlugin, VectorTypeMappingSourcePlugin>();
}
public void Validate(IDbContextOptions options) { }
private sealed class ExtensionInfo : DbContextOptionsExtensionInfo
{
public ExtensionInfo(IDbContextOptionsExtension extension) : base(extension) { }
private new VectorDbContextOptionsExtension Extension
=> (VectorDbContextOptionsExtension)base.Extension;
public override bool IsDatabaseProvider => false;
public override string LogFragment => "using vector ";
public override int GetServiceProviderHashCode()
=> 0;
public override void PopulateDebugInfo(IDictionary<string, string> debugInfo)
{
debugInfo["Pgvector.EntityFrameworkCore:UseVector"] = "1";
}
public override bool ShouldUseSameServiceProvider(DbContextOptionsExtensionInfo other)
=> true;
}
}