Sequence Contains no matching elements

asp.net-mvc-4 entity-framework-6

Question

I have a SubCategory model in an mvc 4 application for a shirt shop and I am wanting to populate the database using seed data when I ran my application, I recieve Error Message: "Sequence Contains no matching elements"

My code:

    var subCategories = new List<SubCategory>
        {
    new SubCategory{Category = categories.Single(s => s.Name == "Animals") , Name "Animal Prints"},
    new SubCategory{Category = categories.Single(s => s.Name == "Animals") , Name = "Bugs"},
        };
1
0
7/11/2014 10:56:18 PM

Accepted Answer

The reason is that you don't have any entry in you categories with name as "Animals" if you wanted to check it

   var categorie = categories.where(s => s.Name == "Animals").SingleOrDefault();

   if(categories == null)
   {
     // db is not seeded with categories do something
   }
   else
   {     

        var subCategories = new List<SubCategory>
        {
           new SubCategory{Category = categorie  , Name "Animal Prints"},
           new SubCategory{Category = categorie  , Name = "Bugs"},
        };
   }
1
6/12/2014 4:33:34 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow