.NET 6 Identity int foreign key cannot target int primary key

Viewed 163

I get this error when I try to add the migration:

The relationship from 'MyCart.Klienti' to 'Klienti.MyCarts' with foreign key properties {'KlientiId' : int} cannot target the primary key {'Id' : string} because it is not compatible. Configure a principal key or a set of foreign key properties with compatible types for this relationship.

Both keys have the same data type - int - so I'm not sure where the problem is.

This is my code:

modelBuilder.Entity<MyCart>(entity =>
            {
                entity.HasKey(e => e.CartItemId)
                    .HasName("PK__MyCart__488B0B0A1FDF5DF6");

                entity.ToTable("MyCart");

                entity.HasOne(d => d.Food)
                    .WithMany(p => p.MyCarts)
                    .HasForeignKey(d => d.FoodId)
                    .OnDelete(DeleteBehavior.ClientSetNull)
                    .HasConstraintName("FK__MyCart__FoodId__4AB81AF0");

                entity.HasOne(d => d.Klienti)
                    .WithMany(p => p.MyCarts)
                    .HasForeignKey(d => d.KlientiId)
                    .OnDelete(DeleteBehavior.ClientSetNull)
                    .HasConstraintName("FK__MyCart__KlientiI__49C3F6B7");
            });

modelBuilder.Entity<Klienti>(entity =>
            {
                entity.ToTable("Klienti");

                entity.Property(e => e.City)
                    .HasMaxLength(30)
                    .IsUnicode(false);

                entity.Property(e => e.Email)
                    .HasMaxLength(60)
                    .IsUnicode(false);

                entity.Property(e => e.Name)
                    .HasMaxLength(20)
                    .IsUnicode(false);

                entity.Property(e => e.Password)
                    .HasMaxLength(30)
                    .IsUnicode(false);

                entity.Property(e => e.PhoneNumber)
                    .HasMaxLength(15)
                    .IsUnicode(false);

                entity.Property(e => e.Role)
                    .HasMaxLength(10)
                    .IsUnicode(false);

                entity.Property(e => e.StreetName)
                    .HasMaxLength(40)
                    .IsUnicode(false);

                entity.Property(e => e.Surname)
                    .HasMaxLength(20)
                    .IsUnicode(false);

                entity.Property(e => e.ZipCode)
                    .HasMaxLength(12)
                    .IsUnicode(false);
            });

public partial class Klienti : IdentityUser
{
    public Klienti()
    {
        MyCarts = new HashSet<MyCart>();
    }

    public int KlientiId { get; set; }
    public string Name { get; set; } = null!;
    public string Surname { get; set; } = null!;
    public string Email { get; set; } = null!;
    public string Password { get; set; } = null!;
    public string PhoneNumber { get; set; } = null!;
    public string StreetName { get; set; } = null!;
    public string ZipCode { get; set; } = null!;
    public string City { get; set; } = null!;
    public string Role { get; set; } = null!;

    public virtual ICollection<MyCart> MyCarts { get; set; }
}

public partial class MyCart
{
    public int CartItemId { get; set; }
    public int KlientiId { get; set; }
    public int FoodId { get; set; }

    public virtual Food Food { get; set; } = null!;
    public virtual Klienti Klienti { get; set; } = null!;
}
0 Answers
Related