到目前为止一切正常,但我已经开始出现这个错误了。你能帮我解决一下代码有什么问题。
我的产品型号
public class Product : EntityBase
{
public Product()
{
this.Photos = new List<Photo>();
this.OrderDetails = new List<OrderDetail>();
this.Suppliers = new List<Supplier>();
this.Comments = new List<Comment>();
this.Ozelliks = new List<Ozellik>();
}
[MaxLength(100, ErrorMessage = "Max 100 karakter."), Required]
public string Name { get; set; }
[Required]
public decimal BuyingPrice { get; set; }
[Required]
public decimal SalesPrice { get; set; }
[Required]
public decimal DiscountedPrice { get; set; }
public decimal VATRatio { get; set; }
[MaxLength(250, ErrorMessage = "Max 3050 karakter."), Required]
public string DescriptionLong { get; set; }
[MaxLength(250, ErrorMessage = "Max 250 karakter."), Required]
public string DescriptionShort { get; set; }
[MaxLength(250, ErrorMessage = "Max 250 karakter."), Required]
public string Brand { get; set; }
[Required]
public string Size { get; set; }
public Gender Gender { get; set; }
public Color Color { get; set; }
public bool IsActive { get; set; }
public decimal? Discount { get; set; }
public int StockQuantity { get; set; }
public int LineNumber { get; set; }
public float CommentAvg { get; set; }
public bool IsNew { get; set; }
public virtual Mainproduct Mainproduct { get; set; }
public virtual List<Campaign> Campaigns{ get; set; }
public virtual Category Category { get; set; }
public virtual List<OrderDetail> OrderDetails { get; set; }
public virtual List<Supplier> Suppliers { get; set; }
public virtual List<Photo> Photos { get; set; }
public virtual List<Comment> Comments { get; set; }
public virtual List<Ozellik> Ozelliks { get; set; }
}
我的规格型号
public class Ozellik : EntityBase
{
[Required]
public int ProductId { get; set; }
[MaxLength(100, ErrorMessage = "Max 100 karakter."), Required(ErrorMessage = "Bu alan boş geçilemez")]
public string Name { get; set; }
[MaxLength(100, ErrorMessage = "Max 100 karakter.")]
public string Description { get; set; }
public bool IsActive { get; set; }
}
我的控制器
public ActionResult Index()
{
List<Product> Products = db.Products.OrderBy(x => x.LineNumber).Where(x => x.IsActive == true).Where(x => x.StockQuantity > 0).ToList();
ViewBag.Categories = db.Categories.ToList();
return View(Products);
}
我的观点
@model IEnumerable<Product>
@foreach (Product item in Model.Where(x => x.Category.Name == "Pasta"))
{
我在这个foreach循环中得到了错误。它在10分钟前工作。只有我改变了我已经为产品型号添加了新属性。我已经从sql server studio手动更新了旧记录的新添加的属性数据。我已检查断点并在控制器中获取5个产品并发送到视图。
我在这里缺少什么?谢谢你的帮助。
答案 0 :(得分:0)
通常我在使用“Single()”类型的命令时看到“Sequence contains no elements”。
两项改进,应该有所帮助:
e.g。 Model.Where(x =&gt; x.Category.Name ==“Pasta”)。ToList()
e.g。 x =&gt; x.Category!= null&amp;&amp; x.Category.Name ==“Pasta”
如果以前工作并且你提到你更新了架构,那么问题可能是一个过时的EF模型,或者你不再拥有在没有返回任何行时抛出错误的数据。