You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We use a custom ValueConverter to map our Undefinable type into the database relevant type. This works as expected as long as TValue is not a reference or nullable type.
Currently EF Core generates two database columns for every reference or nullable type one is our defined and there is an additional shadow foreign key property.
Steps to reproduce
We have an Undefinable struct which tells us if the client provide a value for a property
Using this ValueConverter EF Core uses the correct database type. (This works as expected as long as TValue is not a reference or nullable type)
publicclassUndefinableConverter<TValue>:ValueConverter<Undefinable<TValue>,TValue>{publicUndefinableConverter():base(undefinable =>undefinable.IsDefined?undefinable.Value:default, value =>newUndefinable<TValue>(value)){}}
Note for triage: this fails because the type of the FK is Undefinable<Nullable<Guid>>, while the PK is Undefinable<Guid>. We tallow nullable FKs against non-nullable PK by special handling of nullable types. Generalizing this could get tricky.
Model (EF Core 5.0):
Model:
EntityType: Address
Properties:
Id (Undefinable<Guid>) Required PK AfterSave:Throw
Keys:
Id PK
EntityType: Person
Properties:
Id (Undefinable<Guid>) Required PK AfterSave:Throw
AddressId (Undefinable<Nullable<Guid>>) Required
AddressId1 (no field, Nullable<Undefinable<Guid>>) Shadow FK Index
Navigations:
Address (Address) ToPrincipal Address
Keys:
Id PK
Foreign keys:
Person {'AddressId1'} -> Address {'Id'} ToPrincipal: Address ClientSetNull
Indexes:
AddressId1
I there any possibility to modify the special handling nullable FK against non-nullable PK?
In our case ist would be enough to use the TValue type of the Undefinable struct. This is a urgent problem from us which we need to solve
@alc86 I am not aware of any way to make this work.
ajcvickers
changed the title
The provider type of the ValueConverter is not used for relationships
Allow different types for FK/PK if a value converters to compatible provider types are used
Sep 18, 2020
We use a custom ValueConverter to map our Undefinable type into the database relevant type. This works as expected as long as TValue is not a reference or nullable type.
Currently EF Core generates two database columns for every reference or nullable type one is our defined and there is an additional shadow foreign key property.
Steps to reproduce
We have an Undefinable struct which tells us if the client provide a value for a property
Using this ValueConverter EF Core uses the correct database type. (This works as expected as long as TValue is not a reference or nullable type)
Look at the following relationsship
At the DBContext we use the following snippet to add the UndefinableConverter
EF Core creates a migration with two columns for the Person AddressId:
Further technical details
EF Core version: 3.1.7
Database provider: Npgsql.EntityFrameworkCore.PostgreSQL (3.1.4)
Target framework: .NET Core 3.1
IDE: Visual Studio 2019 16.3
The text was updated successfully, but these errors were encountered: