1
votes

I am having issues running a code first migration using the entity framework while seeding information. I can't seem to figure out what I am doing wrong. Here is the error the package manager console is giving me when I run 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.

There is a larger stack trace which doesn't tell me much more. Anyway here is my Configuration.cs seed code.

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);

Now here are my models

Status

[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; }
}

Category

[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; }
}

Blog

[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; }
}

What am I doing wrong? Thanks in advance for any help.

Edit: After looking into the database I noticed the category is being added but the statuses and blog entry are not when I run the seed.

2
What is AddOrUpdate? That's not an Entity Framework method.Erik Funkenbusch
@MystereMan yes it is.Kyle Trauberman
@KyleTrauberman - No, it's not. However, I did find that it's part of Migrations. Not part of EF itself. tags updated to reflect the use of MigrationsErik Funkenbusch

2 Answers

1
votes

Most likely there is not a record in the Statuses table with Id 1. We can't tell since this is an identity column and as such are not passing in the Id property. So try setting the Status navigation property instead of the StatusID and it should resolve properly when SaveChanges() is called:

var blogPost = new Blog
        {
            CategoryId = 1,
            ...
            Status = statusDraft,
            ...
        };
0
votes

I had this Issue and here is my solution. It is nothing special but checking data before inserting into database.

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]
                });
            }
        }
    }