0

I encountered a problem where I cant seed an SQL script since it has a foreign key constraint, I tried context.SaveChanges() but it isn't working. Is there any way how this can be done?

protected override void Seed(ApplicationDbContext context)
{
    List<Type> types = new List<Type>();
    types.Add(new Type() { Type = "Fair" });
    types.Add(new Type() { Type = "Great" });

    context.Type.AddRange(types);

    context.SaveChanges();

    var baseDir = AppDomain.CurrentDomain.BaseDirectory.Replace("\\bin", string.Empty) + "\\Paths";

    context.Database.ExecuteSqlCommand(File.ReadAllText(baseDir + "\\Types.sql"));
    context.Database.ExecuteSqlCommand(File.ReadAllText(baseDir + "\\Category.sql"));

    base.Seed(context);
}

Model:

public class Type
{
   [Key]
   [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
   public int Id { get; set; }

   public string Title { get; set; }
}
MexicoBoy
  • 47
  • 6

2 Answers2

2

Your Type class has not property with Type name. you must change the Type property to Title

List<Type> types = new List<Type>();

types.Add(new Type() { Title = "Fair" });//NOTE THIS
types.Add(new Type() { Title = "Great" });//NOTE THIS

context.Type.AddRange(types);
context.SaveChanges();

then remove base.Seed(context);

Farhad Zamani
  • 5,381
  • 2
  • 16
  • 41
0

The question has been solved, the problem was that the data generated was not always the same, hence tweeking was made to INSERT the same data (Id) always the same

MexicoBoy
  • 47
  • 6
  • So nobody had any hope of guessing that to help you. Highlights that you need to write better questions and give more info going forward. Good you got it sorted. – Lotok Apr 27 '20 at 22:01