序列不包含匹配元素实体种子错误



以前我有一个实体数据库在工作。出于匆忙,我删除了它,所以我再次运行我的程序(认为它应该像以前一样重新创建和填充数据库)。然而,这次我出现了一个奇怪的错误:System.InvalidOperationException:序列不包含匹配的元素

在我的"SampleData.cs"文件中的这行代码上(种子数据来自的位置):

52:             new List<Card>

知道这里发生了什么吗?

应用程序启动方法:

 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型号

    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; }
}

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));    
        }
    }
}

"我忘了添加一个新的"Special"字段"。

相关内容

最新更新