以前我有一个实体数据库工作。我匆忙删除了它,所以我再次运行我的程序(认为它应该像以前一样重新创建和重新填充数据库)。但是,这一次我得到了一个奇怪的错误: System.InvalidOperationException: Sequence contains no matching element
在我的“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));
}
}
}