Skip to content
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

EF Core Foreign Key Enum-to-Primitive Conversion Issue: "Cannot target primary key because it is not compatible" #2645

Closed
yousafzainasir9 opened this issue Nov 23, 2024 · 4 comments
Labels
question Further information is requested

Comments

@yousafzainasir9
Copy link

yousafzainasir9 commented Nov 23, 2024

I have mapped field to following this. video
entities generated now have enum mapping, but when I try to fetch, I get a runtime error.
basically, my enum field is a foreign key reference.

The relationship from 'TempEntity.TempReferenceEntity' to 'TempReferenceEntity.TempEntities' with foreign key properties {'TempEnumID' : TempEnumType} cannot target the primary key {'TempEnumID' : short} because it is not compatible. Configure a principal key or a set of foreign key properties with compatible types for this relationship.

my enum is of type short EvidenceTypeEnum

also I am generating the conversion

  modelBuilder.Entity<TempEntity>(entity =>
  {
      entity.Property(e => e.TempEnumID)
          .HasConversion<short>() // Converts enum to short
          .HasColumnType("smallint") // Matches the database type
          .IsRequired();
  
      entity.HasOne(d => d.TempReferenceEntity)
          .WithMany(p => p.TempEntities)
          .HasForeignKey(d => d.TempEnumID)
          .OnDelete(DeleteBehavior.ClientSetNull)
          .HasConstraintName("FK_TempEntity_TempReferenceEntity");
  });

is the enum code

public enum TempEnumType : short
{
    TypeA = 1,
    TypeB = 2,
    TypeC = 3
}

public class TempEntity
{
    public int TempEntityID { get; set; } // Primary Key
    public TempEnumType TempEnumID { get; set; } // Foreign Key
    public virtual TempReferenceEntity TempReferenceEntity { get; set; } // Navigation Property
}

public class TempReferenceEntity
{
    public short TempEnumID { get; set; } // Primary Key
    public string Name { get; set; }
    public virtual ICollection<TempEntity> TempEntities { get; set; } // Navigation Property
}
@yousafzainasir9 yousafzainasir9 changed the title Configure a principal key or a set of foreign key properties with compatible types for this relationship. EF Core Foreign Key Enum-to-Primitive Conversion Issue: "Cannot target primary key because it is not compatible" Nov 23, 2024
@ErikEJ
Copy link
Owner

ErikEJ commented Nov 23, 2024

I assume the current solution does not work if the column if a foregin key

@ErikEJ ErikEJ mentioned this issue Nov 24, 2024
16 tasks
@ErikEJ
Copy link
Owner

ErikEJ commented Nov 24, 2024

I have updated the docs here

@ErikEJ ErikEJ closed this as not planned Won't fix, can't repro, duplicate, stale Nov 24, 2024
@ErikEJ ErikEJ added the question Further information is requested label Nov 24, 2024
@yousafzainasir9
Copy link
Author

In order to make it work, I have updated the Primary Key to the enum as well, in this way I am able to set Foreign Key as enum.

public enum TempEnumType : short
{
    TypeA = 1,
    TypeB = 2,
    TypeC = 3
}

public class TempEntity
{
    public int TempEntityID { get; set; } // Primary Key
    public TempEnumType TempEnumID { get; set; } // Foreign Key
    public virtual TempReferenceEntity TempReferenceEntity { get; set; } // Navigation Property
}

public class TempReferenceEntity
{
    public TempEnumType  TempEnumID { get; set; } // Primary Key
    public string Name { get; set; }
    public virtual ICollection<TempEntity> TempEntities { get; set; } // Navigation Property
}

@ErikEJ
Copy link
Owner

ErikEJ commented Nov 25, 2024

Great, and thanks for sharing.

If you like my free tools, I would be very grateful for a rating/review on Visual Studio Marketplace and/or a one-time or monthly sponsorship

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants