情報のシード中にエンティティ フレームワークを使用したコード ファースト マイグレーションの実行に問題があります。私は自分が間違っていることを理解できないようです。これは、update-database を実行したときにパッケージ マネージャー コンソールが表示するエラーです。
Running Seed method.
System.Data.Entity.Infrastructure.DbUpdateException: An error occurred while updating the entries. See the inner exception for details. ---> System.Data.UpdateException: An error occurred while updating the entries. See the inner exception for details. --->
System.Data.SqlClient.SqlException: The INSERT statement conflicted with the FOREIGN KEY constraint "FK_dbo.Blogs_dbo.Statuses_StatusId". The conflict occurred in database "GregGoodwin", table "dbo.Statuses", column 'Id'.
The statement has been terminated.
それ以上のことはわかりませんが、より大きなスタック トレースがあります。とにかく、ここに私のConfiguration.csシードコードがあります。
var cat = new Category { CategoryTitle = "General", PostDate = DateTime.Now, Slug = "general" };
context.Categories.AddOrUpdate(cat);
var statusPub = new Status { StatusTitle = "Published", Slug = "published", PostDate = DateTime.Now };
context.Statuses.AddOrUpdate(statusPub);
var statusDraft = new Status { StatusTitle = "Draft", Slug = "draft", PostDate = DateTime.Now };
context.Statuses.AddOrUpdate(statusDraft);
var blogPost = new Blog
{
CategoryId = 1,
AllowComments = true,
PostDate = DateTime.Now,
PostDescription = "Default post added to database",
PostTitle = "Hello World",
Slug = "hello-world",
PostContent = "<p>Hello to the world</p>",
StatusId = 1,
Tags = "hello,world",
ShortLink = "http://bit.ly/16Qt4wp"
};
context.Blogs.AddOrUpdate(blogPost);
ここに私のモデルがあります
状態
[Table("Statuses")]
public class Status
{
[Key, DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[Required]
public string StatusTitle { get; set; }
public string Slug { get; set; }
[DataType(DataType.Date), Timestamp]
public DateTime PostDate { get; set; }
}
カテゴリー
[Table("Categories")]
public class Category
{
[Key, DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[Required]
public string CategoryTitle { get; set; }
public string Slug { get; set; }
[DataType(DataType.Date), Timestamp]
public DateTime PostDate { get; set; }
}
ブログ
[Table("Blogs")]
public class Blog
{
[Key, DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[Required]
public string PostTitle { get; set; }
[Required]
public string PostDescription { get; set; }
[Required]
public string PostContent { get; set; }
public string Slug { get; set; }
public string Tags { get; set; }
public bool AllowComments { get; set; }
public string ShortLink { get; set; }
[DataType(DataType.Date), Timestamp]
public DateTime PostDate { get; set; }
public int StatusId { get; set; }
public int CategoryId { get; set; }
public virtual Category Category { get; set; }
public virtual Status Status { get; set; }
}
私は何を間違っていますか?助けてくれてありがとう。
編集: データベースを調べた後、カテゴリが追加されていることに気付きましたが、シードを実行したときのステータスとブログ エントリはありません。