MVC音乐商店变化 - “序列包含无元素”错误

时间:2012-01-03 10:21:02

标签: asp.net-mvc entity-framework razor

我想创建自己的基本在线商店,所以我浏览了一下,最后选择修改MVC音乐商店教程,以满足我的需求。

我还处于早期阶段,到目前为止,我的版本与教程版本非常相似,但表和关联类除外。教程有流派,专辑和艺术家 - 我有一个更简单的产品和类别,我已经相应地调整了我的项目,但我收到了错误。

它只发生在StoreController \ Browse部分。当我尝试查看特定类别中的所有项目时,我得到一个System.InvalidOperationException“序列不包含任何元素”

控制器中使用的代码是:

public ActionResult Browse(string cat)
{
var categoryModel = storeDB.Categories.Include("Products")
    .Single(g => g.CatName == cat);

return View(categoryModel);
}

视图如下:

@model TPATK_shop.Models.Category

@{
    ViewBag.Title = "Browse";
}

<h2>Browsing Category: @Model.CatName</h2>

<ul>
    @foreach (var product in Model.Products)
    {
        <li>
            @product.ProdName
        </li>
    }
</ul>

我将所有类和实体设置如下:

namespace TPATK_shop.Models
{
    public partial class Category
    {
        public int CategoryId { get; set; }
        public string CatName { get; set; }
        public string Description { get; set; }

        public List<Product> Products { get; set; }
    }
}

namespace TPATK_shop.Models
{
    public class Product
    {
        public int ProductId { get; set; }
        public int CategoryId { get; set; }
        public string ProdName { get; set; }
        public string Description { get; set; }
        public decimal Price { get; set; }
        public string ImageURL { get; set; }

        public Category Category { get; set; }
    }
}

namespace TPATK_shop.Models
{
    public class TPATKStoreEntities : DbContext
    {
        public DbSet<Product> Products { get; set; }
        public DbSet<Category> Categories { get; set; }
    }
}

我一直在研究这个问题,我似乎无法找到解决方案。我尝试过单一默认但我仍然遇到错误。

我已确认数据库中存在所有数据且密钥正确。

当我的项目启动时,global.asax中的一个条目会触发以下代码来填充数据库(用于测试目的 - 详见演练)

namespace TPATK_shop.Models
{
    public class SampleData :  DropCreateDatabaseAlways<TPATKStoreEntities>
    //public class SampleData : DropCreateDatabaseIfModelChanges<TPATKStoreEntities>
    {
        protected override void Seed(TPATKStoreEntities context)
        {
            var categories = new List<Category>
            {
                new Category { CatName = "50's" , Description = "50's stuff" },
                new Category { CatName = "Weddings" , Description = "Wedding stuff" },
                new Category { CatName = "Dribble Bibs" , Description = "Dribble bibs for kids" },
                new Category { CatName = "Xmas" , Description = "Xmas stuff" },
            };

            new List<Product>
            {
                new Product { ProdName = "Headband 1", Description = "Des for headband 1", Category = categories.Single(g => g.CatName == "50's"), Price = 8.99M, ImageURL = "/Content/Images/placeholder.gif" },
                new Product { ProdName = "Headband 2", Description = "Des for headband 2", Category = categories.Single(g => g.CatName == "Weddings"), Price = 8.99M, ImageURL = "/Content/Images/placeholder.gif" },
                new Product { ProdName = "Headband 3", Description = "Des for headband 3", Category = categories.Single(g => g.CatName == "Dribble Bibs"), Price = 8.99M, ImageURL = "/Content/Images/placeholder.gif" },
                new Product { ProdName = "Headband 4", Description = "Des for headband 4", Category = categories.Single(g => g.CatName == "Xmas"), Price = 8.99M, ImageURL = "/Content/Images/placeholder.gif" },
                new Product { ProdName = "Headband 4", Description = "Des for headband 5", Category = categories.Single(g => g.CatName == "Xmas"), Price = 8.99M, ImageURL = "/Content/Images/placeholder.gif" },
            }.ForEach(a => context.Products.Add(a));
        }
    }
}

有人能看出我收到错误的原因吗?任何帮助都会得到很大的帮助。

3 个答案:

答案 0 :(得分:3)

这是因为提供的cat在数据库中没有匹配的记录。使用SingleOrDefault方法检索实体,如果没有记录,它将返回null

public ActionResult Browse(string cat)
{
    var categoryModel = storeDB.Catagories.Include("Products")
        .SingleOrDefault(g => g.CatName == cat);

    if (categoryModel == null)
       return RedirectToAction("Index");

    return View(categoryModel);
}

可能的情况是MVC无法将值绑定到cat参数并且设置为null。因此,请检查您是否传递了名为cat的查询字符串参数。

答案 1 :(得分:1)

导致错误的拼写错误。而不是“类别”,你使用了“Catagory”。现在,因为您已经使用了POCO Code-first,所以有一个约定,它将Model名称与数据库表格相匹配。由于没有“CATAGORY”这样的词,因此plurization失败。

要使此错误正确,请将Catagory重命名为Category Anywhere,并将Catagories更改为DbContext类中的类别。

你可以装饰你的Catagory类,如下所示

[Table("Catagories"]
public class Catagory

如果您遵循此方法,请将Product Model中的拼写从CategoryId更改为CatagoryId

答案 2 :(得分:0)

看起来你得到了异常,因为使用Single方法意味着你希望返回一个Category,如果没有,那就是一个例外情况。

在这种情况下可能会发生,因为您的播种代码似乎也缺少将每个类别明确添加到上下文中。

也可能存在问题,因为EF Code First系统会尝试根据属性名称匹配键。您有一个产品引用 Catagory 对象,但使用 CategoryID 标识符(带有“e”,而不是“a”)。