'relationship from Table to X with foreign key properties x cannot target the primary key x because it is not compatible
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
