Skip to content

Commit b8e167d

Browse files
ankaneroji
andcommitted
Added support for scaffolding - resolves #44 and resolves #50
Co-authored-by: Shay Rojansky <[email protected]>
1 parent 4de753b commit b8e167d

8 files changed

+88
-3
lines changed

src/Pgvector.EntityFrameworkCore/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.2.2 (unreleased)
2+
3+
- Added support for scaffolding
4+
15
## 0.2.1 (2024-06-25)
26

37
- Added support for `halfvec` and `sparsevec` types

src/Pgvector.EntityFrameworkCore/HalfvecTypeMappingSourcePlugin.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace Pgvector.EntityFrameworkCore;
55
public class HalfvecTypeMappingSourcePlugin : IRelationalTypeMappingSourcePlugin
66
{
77
public RelationalTypeMapping? FindMapping(in RelationalTypeMappingInfo mappingInfo)
8-
=> mappingInfo.ClrType == typeof(HalfVector)
8+
=> mappingInfo.ClrType == typeof(HalfVector) || (mappingInfo.ClrType == null && (mappingInfo.StoreTypeNameBase ?? mappingInfo.StoreTypeName) == "halfvec")
99
? new HalfvecTypeMapping(mappingInfo.StoreTypeName ?? "halfvec")
1010
: null;
1111
}

src/Pgvector.EntityFrameworkCore/Pgvector.EntityFrameworkCore.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
<None Include="..\..\LICENSE.txt" Pack="true" PackagePath="\" />
2121
<None Include="..\..\README.md" Pack="true" PackagePath="\" />
2222
<None Include="..\..\icon.png" Pack="true" PackagePath="\" />
23+
<None Include="build\**\*" Pack="true" PackagePath="build" />
2324
<ProjectReference Include="..\Pgvector\Pgvector.csproj" />
2425
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.0" />
2526
</ItemGroup>

src/Pgvector.EntityFrameworkCore/SparsevecTypeMappingSourcePlugin.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace Pgvector.EntityFrameworkCore;
55
public class SparsevecTypeMappingSourcePlugin : IRelationalTypeMappingSourcePlugin
66
{
77
public RelationalTypeMapping? FindMapping(in RelationalTypeMappingInfo mappingInfo)
8-
=> mappingInfo.ClrType == typeof(SparseVector)
8+
=> mappingInfo.ClrType == typeof(SparseVector) || (mappingInfo.ClrType == null && (mappingInfo.StoreTypeNameBase ?? mappingInfo.StoreTypeName) == "sparsevec")
99
? new SparsevecTypeMapping(mappingInfo.StoreTypeName ?? "sparsevec")
1010
: null;
1111
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using Microsoft.EntityFrameworkCore;
2+
using Microsoft.EntityFrameworkCore.Design;
3+
using Microsoft.EntityFrameworkCore.Scaffolding;
4+
using Npgsql.EntityFrameworkCore.PostgreSQL.Infrastructure;
5+
using System.Reflection;
6+
7+
namespace Pgvector.EntityFrameworkCore;
8+
9+
public class VectorCodeGeneratorPlugin : ProviderCodeGeneratorPlugin
10+
{
11+
private static readonly MethodInfo _useVectorMethodInfo
12+
= typeof(VectorDbContextOptionsBuilderExtensions).GetMethod(
13+
nameof(VectorDbContextOptionsBuilderExtensions.UseVector),
14+
[typeof(NpgsqlDbContextOptionsBuilder)])!;
15+
16+
public override MethodCallCodeFragment GenerateProviderOptions()
17+
=> new(_useVectorMethodInfo);
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using Microsoft.EntityFrameworkCore.Design;
2+
using Microsoft.EntityFrameworkCore.Scaffolding;
3+
using Microsoft.EntityFrameworkCore.Storage;
4+
using Microsoft.Extensions.DependencyInjection;
5+
6+
namespace Pgvector.EntityFrameworkCore;
7+
8+
public class VectorDesignTimeServices : IDesignTimeServices
9+
{
10+
public virtual void ConfigureDesignTimeServices(IServiceCollection serviceCollection)
11+
=> serviceCollection
12+
.AddSingleton<IRelationalTypeMappingSourcePlugin, VectorTypeMappingSourcePlugin>()
13+
.AddSingleton<IRelationalTypeMappingSourcePlugin, HalfvecTypeMappingSourcePlugin>()
14+
.AddSingleton<IRelationalTypeMappingSourcePlugin, SparsevecTypeMappingSourcePlugin>()
15+
.AddSingleton<IProviderCodeGeneratorPlugin, VectorCodeGeneratorPlugin>();
16+
}

src/Pgvector.EntityFrameworkCore/VectorTypeMappingSourcePlugin.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace Pgvector.EntityFrameworkCore;
55
public class VectorTypeMappingSourcePlugin : IRelationalTypeMappingSourcePlugin
66
{
77
public RelationalTypeMapping? FindMapping(in RelationalTypeMappingInfo mappingInfo)
8-
=> mappingInfo.ClrType == typeof(Vector)
8+
=> mappingInfo.ClrType == typeof(Vector) || (mappingInfo.ClrType == null && (mappingInfo.StoreTypeNameBase ?? mappingInfo.StoreTypeName) == "vector")
99
? new VectorTypeMapping(mappingInfo.StoreTypeName ?? "vector")
1010
: null;
1111
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<Project>
2+
<PropertyGroup>
3+
<MSBuildAllProjects>$(MSBuildAllProjects);$(MSBuildThisFileFullPath)</MSBuildAllProjects>
4+
<EFCoreNpgsqlPgvectorFile>$(IntermediateOutputPath)EFCoreNpgsqlPgvector$(DefaultLanguageSourceExtension)</EFCoreNpgsqlPgvectorFile>
5+
</PropertyGroup>
6+
<Choose>
7+
<When Condition="'$(Language)' == 'F#'">
8+
<Choose>
9+
<When Condition="'$(OutputType)' == 'Exe' OR '$(OutputType)' == 'WinExe'">
10+
<PropertyGroup>
11+
<CodeFragmentItemGroup>CompileBefore</CodeFragmentItemGroup>
12+
</PropertyGroup>
13+
</When>
14+
<Otherwise>
15+
<PropertyGroup>
16+
<CodeFragmentItemGroup>CompileAfter</CodeFragmentItemGroup>
17+
</PropertyGroup>
18+
</Otherwise>
19+
</Choose>
20+
</When>
21+
<Otherwise>
22+
<PropertyGroup>
23+
<CodeFragmentItemGroup>Compile</CodeFragmentItemGroup>
24+
</PropertyGroup>
25+
</Otherwise>
26+
</Choose>
27+
<Target Name="AddEFCoreNpgsqlPgvector"
28+
BeforeTargets="CoreCompile"
29+
DependsOnTargets="PrepareForBuild"
30+
Condition="'$(DesignTimeBuild)' != 'True'"
31+
Inputs="$(MSBuildAllProjects)"
32+
Outputs="$(EFCoreNpgsqlPgvectorFile)">
33+
<ItemGroup>
34+
<EFCoreNpgsqlPgvectorServices Include="Microsoft.EntityFrameworkCore.Design.DesignTimeServicesReferenceAttribute">
35+
<_Parameter1>Pgvector.EntityFrameworkCore.VectorDesignTimeServices, Pgvector.EntityFrameworkCore</_Parameter1>
36+
<_Parameter2>Npgsql.EntityFrameworkCore.PostgreSQL</_Parameter2>
37+
</EFCoreNpgsqlPgvectorServices>
38+
</ItemGroup>
39+
<WriteCodeFragment AssemblyAttributes="@(EFCoreNpgsqlPgvectorServices)"
40+
Language="$(Language)"
41+
OutputFile="$(EFCoreNpgsqlPgvectorFile)">
42+
<Output TaskParameter="OutputFile" ItemName="$(CodeFragmentItemGroup)" />
43+
<Output TaskParameter="OutputFile" ItemName="FileWrites" />
44+
</WriteCodeFragment>
45+
</Target>
46+
</Project>

0 commit comments

Comments
 (0)