以前我有一个实体数据库正在运行。我删除了它,出于匆忙,所以我再次运行我的程序(认为它应该像之前那样重新创建和重新填充数据库)。但是,这次我得到了一个奇怪的错误: System.InvalidOperationException:Sequence不包含匹配元素
在我的“SampleData.cs”文件中的这行代码(种子数据来自的位置):
52: new List<Card>
知道这里发生了什么?
App Start方法:
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));
}
}
}
答案 0 :(得分:0)
“我忘了添加一个新的”特殊“字段”。