I've got two classes
public class Category { public int Id { get; set; } public string Name { get; set; } } public class Item { public int Id { get; set; } public sting Name { get; set; } public Category Category { get; set; } } I have EF Migrations and the following seed:
var instockCategory = new Category() { Name = "InStock" }; var outofStockCategory = new Category() { Name = "OutOfStock" }; context.Items.AddOrUpdate( d => d.Name, new Item() { Name = "Item1", Category = instockCategory }, new Item() { Name = "Item2", Category = outofStockCategory }, new Item() { Name = "Item3", Category = outofStockCategory } ); The line "d => d.Name" makes sure that based on the name of the item, there won't be duplicate records when I reseed the database. However, the first time I execute this, two categories are created with id 1 and 2. But the second time I run this, 3 new categories are created! Can I fix this without manually adding every single category first?