以前は、エンティティ データベースが機能していました。急いで削除したので、プログラムを再度実行しました(以前のようにDBを再作成して再投入する必要があると考えました)。ただし、今回は奇妙なエラーが発生しました: 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));
}
}
}