【发布时间】:2014-12-14 19:20:04
【问题描述】:
我有这 2 个类,产品与 ProductType 相关,但是当使用下面的代码时,我看到以下错误。
无法创建“EShop.ClassLibrary.ProductType”类型的常量值。此上下文仅支持原始类型或枚举类型
它们与 ProductType LinkProductTypeId 相关
public class Product
{
[Key]
public virtual int ProductID { get; set; }
public virtual ProductType LinkProductTypeId { get; set; }
public virtual string Name { get; set; }
public virtual string EngName { get; set; }
public virtual RelatedProduct ProductRelated { get; set; }
public virtual decimal BuyCost { get; set; }
public virtual decimal SellCost { get; set; }
}
public class ProductType
{
public virtual int ProductTypeId { get; set; }//id in ef
public virtual string Name { get; set; }
}
运行时代码是这样的:
public static List<SelectListItem> GetProductSelectList()
{
ProductType type = _db.productType.Single(t => t.ProductTypeId == 1);
List<Product> m = (_db.product.Where(r => r.LinkProductTypeId == type) ).ToList();//error line
List<SelectListItem> sa = new List<SelectListItem>();
foreach (Product item in m)
{
sa.Add(new SelectListItem { Text = item.Name, Value = item.ProductID.ToString() });
}
return sa;
}
【问题讨论】:
-
一句警告。寻求帮助时,您需要传递所有信息。例如,“但它失败了”并没有告诉“它是如何失败的”,也没有告诉它失败的地方(哪一行引发了异常)。阅读:stackoverflow.com/help/how-to-ask。
标签: c# asp.net asp.net-mvc