1

我在播种信息时使用实体框架运行代码优先迁移时遇到问题。我似乎无法弄清楚我做错了什么。这是我运行 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; }
}

我究竟做错了什么?提前感谢您的帮助。

编辑:查看数据库后,我注意到正在添加类别,但状态和博客条目在我运行种子时没有。

4

2 回答 2

1

状态表中很可能没有带有 的记录Id 1。我们无法判断,因为这是一个标识列,因此没有传入 Id 属性。因此,尝试设置 Status 导航属性而不是 StatusID,它应该在SaveChanges()被调用时正确解析:

var blogPost = new Blog
        {
            CategoryId = 1,
            ...
            Status = statusDraft,
            ...
        };
于 2013-04-12T02:13:11.527 回答
0

我有这个问题,这是我的解决方案。没有什么特别的,只是在插入数据库之前检查数据。

protected override void Seed(ApplicationDbContext context) {
        var gender = new[] { Gender.NotSpecified, Gender.Male, Gender.Female };
        var dbGender = context.Gender.ToList();
        for (byte i = 0; i < gender.Length; i++) {
            if (dbGender.FirstOrDefault(x => x.Name == gender[i]) != null) {
                context.Gender.Add(new Gender {
                    Id = i,
                    Name = gender[i]
                });
            }
        }
    }
于 2017-09-17T09:46:50.770 回答