CS1061“collectioncollectionbuilder<products, orderproducts>”不包含“hasforeignkey”的定义并且没有可访问的扩展

问题描述 投票:0回答:1

cs1061“collectioncollectionbuilder”不包含“hasforeignkey”的定义,并且找不到接受“collectioncollectionbuilder”类型的第一个参数的可访问扩展方法“hasforeignkey”(您是否缺少 using 指令或程序集引用?)

using CafeApiProject.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CafeApiProject
{
    public class AppDbContext : DbContext
    {
        DbSet<ProductCategory> ProductCategories { get; set; }
        DbSet<Products> Products { get; set; }
        DbSet<OrderProducts> OrderProducts { get; set; }
        DbSet<Employees> Employees { get; set; }
        DbSet<Orders> Orders { get; set; }
        DbSet<Customers> customers { get; set; }
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=CafeDb;Trusted_Connection=True;");
        }


        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Products>()
               .HasOne(p => p.Category)
               .WithMany(c => c.Products)
               .HasForeignKey(p => p.CategoryId);
            modelBuilder.Entity<OrderProducts>()
                .HasMany(op => op.Product)
                .WithMany(c => c.Orders)
                .HasForeignKey(op => new { op.OrderId, op.ProductId });
            modelBuilder.Entity<Orders>()
                .HasOne(e => e.Employee)
                .WithMany(c => c.Orders)
                .HasForeignKey(e => e.EmployeeId);
            modelBuilder.Entity<Orders>()
                .HasOne(c => c.Customer)
                .WithMany(c => c.Orders)
                .HasForeignKey(p => p.CustomerId);
            modelBuilder.Entity<Orders>()
                .HasOne(p => p.Order)
                .WithMany(c => c.Orders)
                .HasForeignKey(p => p.OrderId );






        }
    }
}


namespace CafeApiProject.Models
{
    public class Orders
    {
        [Key]
        public Guid OrderId { get; set; }
        [Required]
        [ForeignKey("OrderId")]
        public OrderProducts Order { get; set; }
        public Guid CustomerId { get; set; }
        [Required]
        [ForeignKey("Id")]
        public Customers Customer { get; set; }
        
        public Guid EmployeeId { get; set; }
        [Required]
        [ForeignKey("EmployeeId")]
        public Employees Employee { get; set; }
        public string OrderList { get; set; }

    }
}
namespace CafeApiProject.Models
{ [PrimaryKey(nameof(OrderId), nameof(ProductId))]
    public class OrderProducts
    {
       
        public Guid OrderId { get; set; }
        [Required]
        
        public Guid ProductId { get; set; }
        [ForeignKey("ProductId")]
        public ICollection<Products> Product { get; set; }   
        public ICollection<Orders> Orders { get; set; }
    }
}


namespace CafeApiProject.Models
{
    public class Products
    {
        [Key]
        public Guid ProductId { get; set; }
        [Required]
        
        public string Name { get; set; }
        [Required]
        public int Article { get; set; }
        [Required]
        public int Price { get; set; }
        public Guid CategoryId { get; set; }
        [ForeignKey("CategoryId")]
        public ProductCategory Category { get; set; }
        public ICollection<OrderProducts> Orders { get; set; }
    }
        
}

请帮忙 尝试添加迁移时也出现错误: “实体类型“OrderProducts”需要定义主键。如果您打算使用无键实体类型,请在“OnModelCreating”中调用“HasNoKey”。有关无键实体类型的更多信息,请参阅https://go。 microsoft.com/fwlink/?linkid=2141943。”

c# entity-framework asp.net-core entity-framework-core
1个回答
0
投票

从您的实体类和 Fluent API 中,我看到生成

OrderProducts
实体存在很多问题,该实体是
Order
Product
实体之间多对多关系的关联表。

订单:

  1. Orders
    实体应该具有一对多
    OrderProducts

  2. 这似乎是一种冲突,您声明了

    CustomerId
    列,但您将
    ForeignKey
    属性与
    Id
    应用于相关的
    Customer
    属性。

订购产品:

  1. EF Core 不支持使用复合键的

    Key
    属性。相反,您应该在 Fluent API 中使用
    HasKey
    方法
    来完成此操作。

  2. OrderProducts
    应分别指代单个
    Orders
    Products
    实体,而不是多个。

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Products>()
        .HasOne(p => p.Category)
        .WithMany(c => c.Products)
        .HasForeignKey(p => p.CategoryId);

    modelBuilder.Entity<Orders>()
        .HasOne(e => e.Employee)
        .WithMany(c => c.Orders)
        .HasForeignKey(e => e.EmployeeId);

    modelBuilder.Entity<Orders>()
        .HasOne(c => c.Customer)
        .WithMany(c => c.Orders)
        .HasForeignKey(p => p.CustomerId);

    modelBuilder.Entity<OrderProducts>()
        .HasKey(op => new { op.OrderId, op.ProductId });

    modelBuilder.Entity<OrderProducts>()
        .HasOne(op => op.Product)
        .WithMany(p => p.Orders)
        .HasForeignKey(op => op.OrderId);

    modelBuilder.Entity<OrderProducts>()
        .HasOne(op => op.Order)
        .WithMany(o => p.OrderProducts)
        .HasForeignKey(op => op.OrderId);
}
public class Orders
{
    [Key]
    public Guid OrderId { get; set; }

    public ICollection<OrderProducts> OrderProducts { get; set; }

    [Required]
    public Guid CustomerId { get; set; }
    
    [ForeignKey("CustomerId")]  // Id for storing CustomerId, really?
    public Customers Customer { get; set; }
        
    [Required]
    public Guid EmployeeId { get; set; }

    [ForeignKey("EmployeeId")]
    public Employees Employee { get; set; }
    public string OrderList { get; set; }
}

public class OrderProducts
{
    [Required]
    public Guid OrderId { get; set; }

    [Required]
    public Guid ProductId { get; set; }

    public Products Product { get; set; }  
 
    public Orders Order { get; set; }
}

public class Products
{
    [Key]
    public Guid ProductId { get; set; }
    [Required]
    public string Name { get; set; }
    [Required]
    public int Article { get; set; }
    [Required]
    public int Price { get; set; }
    public Guid CategoryId { get; set; }
    [ForeignKey("CategoryId")]
    public ProductCategory Category { get; set; }

    public ICollection<OrderProducts> Orders { get; set; }
}
© www.soinside.com 2019 - 2024. All rights reserved.