指定的类型成员'产品' LINQ to Entities不支持

时间:2014-04-07 01:50:14

标签: c# asp.net-mvc linq entity-framework

我收到错误'产品' LINQ to Entities不支持在尝试获取篮子总数时使用cartItems.Product.UnitCost。

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

ShoppingBasket Class

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

有人可以更清楚地解释问题以及我如何解决这个问题?谢谢!

1 个答案:

答案 0 :(得分:0)

我认为这是因为实体框架尚未完全创建ShoppingBasketProduct之间的关系。尝试添加:

public virtual ICollection<ShoppingBasket> ShoppingBaskets { get; set; }

Product班。