从 Table 到具有外键属性 x 的 X 的关系不能以主键 x 为目标,因为它不兼容

relationship from Table to X with foreign key properties x cannot target the primary key x because it is not compatible

每当我创建数据库并启动时(没有迁移)它都会触发一个错误,上面写着

从 'OrderProducts.Product' 到 'Product' 与外键属性 {'IDProduct' : int} 的关系不能以主键 {'IDProduct' : Guid} 为目标,因为它不是兼容的。为此关系配置主键或一组兼容的外键属性

而且我似乎无法找到一种方法来正确配置外键也是此 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; }
         
    }

在 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();
        }

我想做的是多对多的关系来创建一个没有任何迁移的数据库,但我需要 select 这些列是 PK 和 FK,与一种查找相同 table 订单

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

错误信息很清楚,使用FluentAPI配置主键,会生成int类型的主键,但是在Order和Product中class, 可以看到主键是Guid类型,所以会出现上面的not compatible error.

针对这个问题,可以在OrderProducts中设置外键数据类型class,像这样:

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; }
}