Home > Software engineering >  relationship from Table to X with foreign key properties x cannot target the primary key x because i
relationship from Table to X with foreign key properties x cannot target the primary key x because i

Time:02-05

Whenever I am creating a database and feeding on startup(without migrations) It has triggered an error that says

The relationship from 'OrderProducts.Product' to 'Product' with foreign key properties {'IDProduct' : int} cannot target the primary key {'IDProduct' : Guid} because it is not compatible. Configure a principal key or a set of compatible foreign key properties for this relationship

and I cant seem to find a way to correctly configure that the foreign keys are also primary key on this table


    public class OrderProducts
    {
  

        [ForeignKey("IDProduct")]
        public virtual Producto Product {get;set;}=
        
        [ForeignKey("IDOrder")]
        public virtual Orden Orden {get;set;}
    }
    public class Product
    {
        [Required]
        [Key]
        public Guid IDProduct { get; set; }

        [Required]
        [MaxLength(200, ErrorMessage = "Name cant exceed 500 chars")]
        public string Name{ get; set; }

        [Required]
        [Column(TypeName = "decimal(29, 2)")]
        [Range(typeof(decimal),"0", "79228162514264337593543950335", ErrorMessage = "Price Limit between 0 - 79228162514264337593543950335")]
        public decimal Price{ get; set; }

        [Required]
        public ProductTypeEnum ProductType { get; set; }

      
        [MaxLength(200, ErrorMessage = "Name cant exceed 500 chars")]
        public string Description{ get; set; }

        [Required]
        public ulong Stock{ get; set; }

        [Required]
        public string PhotoPath{ get; set; }

        [Required]
        public Gender  Gender {get;set;}

        [Required]
        public TallasDeProductoEnum Talla {get;set;}
        [Required]
        public Guid Code{ get; set; }

    }```

```cs  
     public class Order
    {
        
        [Key]
        public Guid IDOrder { get; set; }

        [Required]
        public ulong Qty {get;set;}

        [ForeignKey("IDClient")]
        public virtual Client Client { get; set; }
        //public virtual List<Product> Productos {get;set;}

        public decimal Total { get; set; }

        public DateTime Date{get;set;}

        public string IdPayment {get;set;}

        public PaymentTypeEnum PaymentType {get;set;}

        //public   List<OrdenProductos> OrderProducts  { get; set; }
         
    }

in AppDbContext

 protected override void OnModelCreating(ModelBuilder modelBuilder)
        {

            modelBuilder.Entity<OrdenProductos> 
              ().HasKey(nameof(Producto.IDProducto),nameof(Orden.IDOrden));
       
                modelBuilder.Entity<SEOModel>().HasNoKey();
            base.OnModelCreating(modelBuilder);
       
           
            modelBuilder.Seed();
        }

what i want to do is a many to many relationship to create a database without any migration but i need to select these columns are PK and FK at the same as a kind of lookup table for orders

CodePudding user response:

public class OrderProducts
{ 
    [ForeignKey("IDProduct")]
    public virtual Producto Product {get;set;} 
    [ForeignKey("IDOrder")]
    public virtual Orden Orden {get;set;}
}
public class Order
{        
    [Key]
    public Guid IDOrder { get; set; }
    ...
}
public class Product
{
    [Required]
    [Key]
    public Guid IDProduct { get; set; }
    ...
}

The relationship from 'OrderProducts.Product' to 'Product' with foreign key properties {'IDProduct' : int} cannot target the primary key {'IDProduct' : Guid} because it is not compatible. Configure a principal key or a set of compatible foreign key properties for this relationship

The error message is clear, by using the Fluent API to configure the primary key, it will generate a primary key with int type, but in the Order and Product class, we can see the primary key is the Guid type, so it will show the above not compatible error.

To the this issue, you can set the foreign key data type in the OrderProducts class, like this:

public class OrderProducts
{
    public Guid IDProduct { get; set; }
    [ForeignKey("IDProduct")]
    public virtual Product Product { get; set; }

    public Guid IDOrder { get; set; }
    [ForeignKey("IDOrder")]
    public virtual Order Orden { get; set; }
}
  •  Tags:  
  • Related