指定的类型成员“产品”在 LINQ to 实体中不受支持
本文关键字:实体 to LINQ 支持 类型 成员 产品 | 更新日期: 2023-09-27 17:56:56
我在尝试获取购物篮总数时使用 cartItems.Product.UnitCost 时收到错误"产品"在 LINQ to Entities 中不受支持。
public decimal GetTotal()
{
//Multiply price by count of item to get price for each item in cart and them sum all prices up
decimal? total = (from cartItems in db.ShoppingBaskets where cartItems.BasketID == ShoppingCartID select (int?)cartItems.BasketQuantity * cartItems.Product.UnitCost).Sum();
return total ?? decimal.Zero;
}
然后,我尝试拆分查询以查看是否可以解决问题
int? quantity = (from cartItems in db.ShoppingBaskets where cartItems.BasketID == ShoppingCartID select (int?)cartItems.BasketQuantity).Sum();
decimal? price = (from cartItems in db.ShoppingBaskets where cartItems.BasketID == ShoppingCartID select cartItems.Product.UnitCost).Sum();
我在第二个带有"产品"的单独查询中遇到了同样的问题。
产品类别
public partial class Product
{
public Product()
{
this.OrderCheckouts = new HashSet<OrderCheckout>();
this.ProductReviews = new HashSet<ProductReview>();
}
public int ProductID { get; set; }
public int CategoryID { get; set; }
public int ManufacturerID { get; set; }
public string ProductName { get; set; }
public string ProductCode { get; set; }
public decimal UnitCost { get; set; }
public int UnitQuantity { get; set; }
public string ProductDescription { get; set; }
public string ProductImage { get; set; }
public virtual Category Category { get; set; }
public virtual Manufacturer Manufacturer { get; set; }
public virtual ICollection<OrderCheckout> OrderCheckouts { get; set; }
public virtual ICollection<ProductReview> ProductReviews { get; set; }
}
购物篮类
public partial class ShoppingBasket
{
public int CartID { get; set; }
public string BasketID { get; set; }
public int BasketQuantity { get; set; }
public int ProductID { get; set; }
public virtual Product Product { get; set; }
}
有人可以更清楚地解释问题以及我如何解决这个问题吗?谢谢!
我认为这是因为实体框架尚未完全创建ShoppingBasket
和Product
之间的关系。尝试添加:
public virtual ICollection<ShoppingBasket> ShoppingBaskets { get; set; }
到Product
类。