-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improvements to EF type mapping #50
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
11 changes: 0 additions & 11 deletions
11
src/Pgvector.EntityFrameworkCore/HalfvecTypeMappingSourcePlugin.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
11 changes: 0 additions & 11 deletions
11
src/Pgvector.EntityFrameworkCore/SparsevecTypeMappingSourcePlugin.cs
This file was deleted.
Oops, something went wrong.
26 changes: 26 additions & 0 deletions
26
src/Pgvector.EntityFrameworkCore/VectorCodeGeneratorPlugin.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using System; | ||
using System.Reflection; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.Design; | ||
using Microsoft.EntityFrameworkCore.Scaffolding; | ||
using Npgsql.EntityFrameworkCore.PostgreSQL.Infrastructure; | ||
|
||
namespace Pgvector.EntityFrameworkCore; | ||
|
||
public class VectorCodeGeneratorPlugin : ProviderCodeGeneratorPlugin | ||
{ | ||
private static readonly MethodInfo _useVectorMethodInfo | ||
= typeof(VectorDbContextOptionsBuilderExtensions).GetMethod( | ||
nameof(VectorDbContextOptionsBuilderExtensions.UseVector), | ||
[typeof(NpgsqlDbContextOptionsBuilder)])!; | ||
|
||
/// <summary> | ||
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to | ||
/// the same compatibility standards as public APIs. It may be changed or removed without notice in | ||
/// any release. You should only use it directly in your code with extreme caution and knowing that | ||
/// doing so can result in application failures when updating to a new Entity Framework Core release. | ||
/// </summary> | ||
public override MethodCallCodeFragment GenerateProviderOptions() | ||
=> new(_useVectorMethodInfo); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/Pgvector.EntityFrameworkCore/VectorDesignTimeServices.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using Microsoft.EntityFrameworkCore.Design; | ||
using Microsoft.EntityFrameworkCore.Scaffolding; | ||
using Microsoft.EntityFrameworkCore.Storage; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Pgvector.EntityFrameworkCore; | ||
|
||
public class VectorDesignTimeServices : IDesignTimeServices | ||
{ | ||
public virtual void ConfigureDesignTimeServices(IServiceCollection serviceCollection) | ||
=> serviceCollection | ||
.AddSingleton<IRelationalTypeMappingSourcePlugin, VectorTypeMappingSourcePlugin>() | ||
.AddSingleton<IProviderCodeGeneratorPlugin, VectorCodeGeneratorPlugin>(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
src/Pgvector.EntityFrameworkCore/build/netstandard2.0/Pgvector.EntityFrameworkCore.targets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<Project> | ||
<PropertyGroup> | ||
<MSBuildAllProjects>$(MSBuildAllProjects);$(MSBuildThisFileFullPath)</MSBuildAllProjects> | ||
<EFCoreNpgsqlPgvectorFile>$(IntermediateOutputPath)EFCoreNpgsqlPgvector$(DefaultLanguageSourceExtension)</EFCoreNpgsqlPgvectorFile> | ||
</PropertyGroup> | ||
<Choose> | ||
<When Condition="'$(Language)' == 'F#'"> | ||
<Choose> | ||
<When Condition="'$(OutputType)' == 'Exe' OR '$(OutputType)' == 'WinExe'"> | ||
<PropertyGroup> | ||
<CodeFragmentItemGroup>CompileBefore</CodeFragmentItemGroup> | ||
</PropertyGroup> | ||
</When> | ||
<Otherwise> | ||
<PropertyGroup> | ||
<CodeFragmentItemGroup>CompileAfter</CodeFragmentItemGroup> | ||
</PropertyGroup> | ||
</Otherwise> | ||
</Choose> | ||
</When> | ||
<Otherwise> | ||
<PropertyGroup> | ||
<CodeFragmentItemGroup>Compile</CodeFragmentItemGroup> | ||
</PropertyGroup> | ||
</Otherwise> | ||
</Choose> | ||
<Target Name="AddEFCoreNpgsqlPgvector" | ||
BeforeTargets="CoreCompile" | ||
DependsOnTargets="PrepareForBuild" | ||
Condition="'$(DesignTimeBuild)' != 'True'" | ||
Inputs="$(MSBuildAllProjects)" | ||
Outputs="$(EFCoreNpgsqlPgvectorFile)"> | ||
<ItemGroup> | ||
<EFCoreNpgsqlPgvectorServices Include="Microsoft.EntityFrameworkCore.Design.DesignTimeServicesReferenceAttribute"> | ||
<_Parameter1>Pgvector.EntityFrameworkCore.VectorDesignTimeServices, Pgvector.EntityFrameworkCore</_Parameter1> | ||
<_Parameter2>Npgsql.EntityFrameworkCore.PostgreSQL</_Parameter2> | ||
</EFCoreNpgsqlPgvectorServices> | ||
</ItemGroup> | ||
<WriteCodeFragment AssemblyAttributes="@(EFCoreNpgsqlPgvectorServices)" | ||
Language="$(Language)" | ||
OutputFile="$(EFCoreNpgsqlPgvectorFile)"> | ||
<Output TaskParameter="OutputFile" ItemName="$(CodeFragmentItemGroup)" /> | ||
<Output TaskParameter="OutputFile" ItemName="FileWrites" /> | ||
</WriteCodeFragment> | ||
</Target> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new logic for this method seems a bit complex. Thoughts on checking the
ClrType
first, then falling back toStoreTypeName
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The issue is that users may provide both a store type and a CLR type - this can be done by annotating a Vector property in the model with a store type (e.g.
vector(3)
). In that case we want to go into the top piece of logic, looking at the user-provided string (which may include the dimension), and not just look at the CLR type.So there's basically three possibilities here:
[Column(TypeName = "sparsevec")]
on a Vector property by accident.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the third case, I think it's better to use the CLR type (at least for now) for backwards compatibility.
Also, the error message when there is a mismatch is a bit confusing.
outputs
(however,
HalfVector
is a valid type)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you saying that if a user specifies an incompatible combination, e.g. a Vector (or even string) CLR type with
halfvec
as the store type, we should just ignore and return an incorrect type mapping? That is bound to fail later anyway (when you try to actually read/write the thing), only with a more cryptic message - so I don't think there should be a backwards compatibility concern... Or am I missing something?FWIW this behavior is what's generally done in EF provider and other extensions.
Yeah, that's something that should be improved on the EF side - can you open an issue on the EF repo?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's odd - is there any sort of implicit casting between the vector types? Even if so, try using
string
as the CLR type or similar.In any case, I'd consider this a bug - the user is specifying an invalid combination of store and CLR types. But if you really prefer, I can remove the compatibility check for the CLR type, and make the store type be the only thing that matters once it's specified. Let me know.