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
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
}
The text was updated successfully, but these errors were encountered:
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
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.
my enum is of type short EvidenceTypeEnum
also I am generating the conversion
is the enum code
The text was updated successfully, but these errors were encountered: