Home > other >  cannot target the primary key because it is not compatible
cannot target the primary key because it is not compatible

Time:10-13

I have a problem when I'm going to Communicate between two table in Ef core and relation is one to many. I want to solve it without change type primary key.my error message is below

The relationship from 'TMSGCategory.TemporaryMessage' to 'TemporaryMessage.TMSGCategories' with foreign key properties {'MsgCategoryId' : short} cannot target the primary key {'Id' : int} because it is not compatible. Configure a principal key or a set of compatible foreign key properties for this relationship.

public class TemporaryMessageConfiguration:IEntityTypeConfiguration<TemporaryMessage>
{
    public void Configure(EntityTypeBuilder<TemporaryMessage> builder)
    {
        builder.HasKey(x => x.Id);

        builder.HasMany(a => a.TMSGCategories).WithOne(a => 
        a.TemporaryMessage).HasForeignKey(a => a.MsgCategoryId);
        
    }
}
public class TMSGCategoryConfiguration:IEntityTypeConfiguration<TMSGCategory>
{
    public void Configure(EntityTypeBuilder<TMSGCategory> builder)
    {   
        builder.HasKey(x => x.MsgCategoryId);
        builder.HasOne(a => a.TemporaryMessage).WithMany(a => a.TMSGCategories);
    }
}

CodePudding user response:

you can set TemporaryMessage.MsgCategoryId and TMSGCategories.Id to int16 in code first and problem solve. You cannot convert int to int16 and both data type must be same in efcore relationship.

  • Related