我正在尝试在我的两个实体Order和Item之间配置一对多关系。 但是,我收到以下错误:
错误CS1061'string'不包含'Orders'的定义,并且找不到接受'string'类型的第一个参数的可访问扩展方法'Orders'(您是否缺少using指令或程序集引用? )
下面是我的 DbContext 的代码:
using Microsoft.EntityFrameworkCore;
using OrderEase.Models;
namespace OrderEase.Data
{
public class OrderDbContext:DbContext
{
public OrderDbContext(DbContextOptions<OrderDbContext> options) : base(options)
{
}
//Configure Relationships
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Order>().HasOne(o => o.Item).WithMany(i => i.Orders).HasForeignKey(o => o.ItemID);
base.OnModelCreating(modelBuilder);
}
}
}
在下面的 Item 类中添加了导航属性:
using System.ComponentModel.DataAnnotations;
namespace OrderEase.Models
{
public class Item
{
[Key]
public int ItemID { get; set; }
[Required(ErrorMessage = "Item name is required")]
[Display(Name = "Item Name")]
public string ItemName { get; set; }
[Required(ErrorMessage = "Item price is required")]
[DataType(DataType.Currency)]
[Display(Name = "Price")]
public decimal Price { get; set; }
[Required(ErrorMessage = "Quantity in stock is required")]
[Range(0, int.MaxValue, ErrorMessage = "Quantity must be non-negative")]
public int QuantityInStock { get; set; }
//One-to-Many Relationship with Order
public int OrderID { get; set; } //Foreign Key referecing Order
public ICollection<Order> Orders { get; set; }
}
}
这是我的订单模型:
using Microsoft.EntityFrameworkCore.Metadata.Internal;
使用OrderEase.Data; 使用 System.ComponentModel.DataAnnotations;
命名空间 OrderEase.Models { 公开课订单 { [钥匙] 公共整数?订单ID { 获取;放; }
[Required]
public string Item { get; set; }
[Range(1, int.MaxValue, ErrorMessage = "Quantity must be greater than 0.")]
public int Quantity { get; set; }
[Required]
[DataType(DataType.Currency)]
public decimal TotalPrice { get; set; }
[Required]
[DataType(DataType.Date)]
[Display(Name = "Order Date")]
public DateTime OrderDate { get; set; }
[Required]
[DataType(DataType.Date)]
[Display(Name = "Delivery Date")]
public DateTime DeliveryDate { get; set; }
[Required]
public string Supplier { get; set; }
public OrderStatus OrderStatus { get; set; }
//Relationship
public List<Item> Items { get; set; }
}
}
如果我做错了,请多多指教。 我也检查了我的名字空间。一切看起来都很好。
我给你一个例子:
public class Order
{
//...Other properties
public string Item { get; set; } //Remove this line
public List<Item> Items { get; set; } //Remove this line
public ICollection<Item> Items { get; set; } //Add this row to your Order model
}
public class Item
{
//...Other properties
public int OrderId { get; set; } //Add this line
public virtual Order Order { get; set; } //Add this line
public ICollection<Order> Orders { get; set; } //Remove this line
}
在创建模型时,像这样更改代码:
namespace OrderEase.Data
{
public class OrderDbContext:DbContext
{
public OrderDbContext(DbContextOptions<OrderDbContext> options) : base(options)
{
}
//Configure Relationships
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Order>()
.HasMany(ur => ur.Items)
.WithOne(u => u.Order)
.HasForeignKey(ur => ur.OrderId)
.IsRequired();
base.OnModelCreating(modelBuilder);
}
}
}