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
The issue starts when I try to create FK to ListItem
publicpartialclassProject{publiclongId{get;set;}publicstringName{get;set;}// StatuspublicEListTypeStatusListTypeId{get;set;}publicvirtualListTypeStatusListType{get;set;}publicEProjectStatusStatusId{get;set;}// FK using enum but PK is an intpublicvirtualListItemStatus{get;set;}// PhasepublicEListTypePhaseListTypeId{get;set;}publicvirtualListTypePhaseListType{get;set;}publicEProjectPhasePhaseId{get;set;}// FK using enum but PK is an intpublicvirtualListItemPhase{get;set;}}
As one can observe, Project.StatusId and Project.PhaseId have been defined using their individual enums in order to restric range. However they are converted to integers in the model.
When I try to generate the migration it will throw the follow error:
The relationship from 'Project.Phase' to 'ListItem' with foreign key properties {'PhaseListTypeId' : EListType, 'PhaseId' : EProjectPhase} cannot target the primary key {'ListTypeId' : EListType, 'Id' : int} because it is not compatible. Configure a principal key or a set of compatible foreign key properties for this relationship.
It complains that int and EProjectPhase (enum) aren't compatible even though they are.
The two workarounds that I've found so far are:
Define my FKs as integers however I loose the numeric range restriction provided by enums. Not a good option for me.
Don't define the FKs in the model. Instead manually defined the FKs in the Migration
// Manually add this to Migration UppublicstaticclassCustomMigrationUp{publicstaticvoidHackInForeighKeys(thisMigrationBuildermigrationBuilder){migrationBuilder.AddForeignKey(name:"FK_Project_ListType_PhaseListTypeId_PhaseId",table:"Project",columns:new[]{"PhaseListTypeId","PhaseId"},principalTable:"ListItem",principalColumns:new[]{"ListTypeId","Id"});migrationBuilder.AddForeignKey(name:"FK_Project_ListType_StatusListTypeId_StatusId",table:"Project",columns:new[]{"StatusListTypeId","StatusId"},principalTable:"ListItem",principalColumns:new[]{"ListTypeId","Id"});}}
The schema generated using ints all the way through and the schema generated by workaround number 2 are identical however option 2 is laborous.
Shouldn't EF allow enums and integers to be used interchangly if .HasConvertion() has been described in the model?
@armaniehs EF Core requires that the type of FK properties matches the type of PK properties, other than nullability.
Note from triage: putting this in the backlog to consider relaxing this for enums and other types which could match with simple convert nodes in the change tracking expression trees.
G'day EF team!
I've created a project to reproduce this issue in here
https://github.com/armaniehs/enum-issue-ef-5
I've defined my system pre-defined lists as groups of enums
and then for each group I defined the individual list items
which are then mapped into ListType and ListItem tables
ListItem.Id is defined as integer because the ids are are defined into separate enums, not a big deal so far.
ListItem also has a composite PK
The issue starts when I try to create FK to ListItem
As one can observe, Project.StatusId and Project.PhaseId have been defined using their individual enums in order to restric range. However they are converted to integers in the model.
When I try to generate the migration it will throw the follow error:
The relationship from 'Project.Phase' to 'ListItem' with foreign key properties {'PhaseListTypeId' : EListType, 'PhaseId' : EProjectPhase} cannot target the primary key {'ListTypeId' : EListType, 'Id' : int} because it is not compatible. Configure a principal key or a set of compatible foreign key properties for this relationship.
It complains that int and EProjectPhase (enum) aren't compatible even though they are.
The two workarounds that I've found so far are:
Define my FKs as integers however I loose the numeric range restriction provided by enums. Not a good option for me.
Don't define the FKs in the model. Instead manually defined the FKs in the Migration
The schema generated using ints all the way through and the schema generated by workaround number 2 are identical however option 2 is laborous.
Shouldn't EF allow enums and integers to be used interchangly if .HasConvertion() has been described in the model?
EF Core version: 5.0.3
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 5.0
The text was updated successfully, but these errors were encountered: