Question

Previously I had an Entity Database working. I deleted it, out of haste, so I went to run my program again (thinking it should recreate and repopulate the DB like it did before). However, this time I got a weird error: System.InvalidOperationException: Sequence contains no matching element

on this line of code in my "SampleData.cs" file (location where seed data comes from):

52:             new List<Card>

ANY idea what is happening here?

App Start Method:

 protected void Application_Start()
    {
        System.Data.Entity.Database.SetInitializer(new 
            GoStopPrimer.Models.SampleData());
        BundleConfig.RegisterBundles(BundleTable.Bundles);
        AreaRegistration.RegisterAllAreas();

        RegisterGlobalFilters(GlobalFilters.Filters);
        RegisterRoutes(RouteTable.Routes);
    }

GoStopEntities.cs

public class GoStopEntities : DbContext
{
    public DbSet<Card> Cards { get; set; }
    public DbSet<CardType> CardTypes { get; set; }
    public DbSet<Month> Months { get; set; }
    public DbSet<Special> Specials { get; set; }
}

Card.cs Model

    public class Card
{
    public int CardId { get; set; }
    public int CardTypeId { get; set; }
    public int MonthId { get; set; }
    public string Name { get; set; }
    public string CardArtUrl { get; set; }
    public virtual CardType CardType { get; set; }
    public virtual Month Month { get; set; }
    public virtual Special Special { get; set; }
}

Snippet of SampleData.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;

namespace GoStopPrimer.Models
{
    public class SampleData : DropCreateDatabaseIfModelChanges<GoStopEntities>
    {
        protected override void Seed(GoStopEntities context)
        {
            var cardTypes = new List<CardType>
            {
                new CardType { Name = "kwang" },       
            };

            var months = new List<Month>
            {
                new Month { Name = "January" },           
            };

            var specials = new List<Special>
            {    
                new Special { Name = "None" },    
            };

            new List<Card>
            {
                new Card { Name = "Pine", CardType = cardTypes.Single(c => c.Name == "kwang"), Month = months.Single(m => m.Name == "January"), CardArtUrl = "/Content/Images/Cards/jan1.gif", Special = specials.Single(s => s.Name == "None") },
                new Card { Name = "Willow/Rain", CardType = cardTypes.Single(c => c.Name == "kwang"), Month = months.Single(m => m.Name == "January"), CardArtUrl = "/Content/Images/Cards/dec4.gif", Special = specials.Single(s => s.Name == "None") },
            }.ForEach(c => context.Cards.Add(c));    
        }
    }
}
Was it helpful?

Solution

"I had forgotten to add a new "Special" field".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top