I created some models, added the migration and then did an update database operation, though at my last update database operation I got the error message saying:
Sequence contains more than one element
Below you can find my migration configuration:
context.Categories.AddOrUpdate(p => p.CategoryName,
new Category
{
CategoryName = "Sport"
},
new Category
{
CategoryName = "Music"
}
);
context.Subcategories.AddOrUpdate(p => p.SubcategoryName,
new Subcategory
{
SubcategoryName = "Football"
},
new Subcategory
{
SubcategoryName = "Basketball"
},
new Subcategory
{
SubcategoryName = "Piano"
},
new Subcategory
{
SubcategoryName = "Violin"
}
);
context.Services.AddOrUpdate(p => p.ServiceType,
new Service
{
ServiceType = "Football player",
Category = { CategoryName = "Sport" },
Subcategory = { SubcategoryName = "Football" }
},
new Service
{
ServiceType = "Piano lessons",
Category = { CategoryName = "Music" },
Subcategory = { SubcategoryName = "Piano" }
}
);
The problem occurs with when I add new Services. I already have categories and subcategories, and if I do like Category = new Category { CategoryName = "Music" }
then it works but I get Music entry twice in my database (for this example). I want to use the already added categories and subcategories. Below also you can find my models definitions.
public class Category
{
[Key]
public int CategoryID { get; set; }
public string CategoryName { get; set; }
}
// Subcategory is defined the same way...
public class Service
{
public int ServiceID { get; set; }
public string ServiceType { get; set; }
public virtual Category Category { get; set; }
public virtual Subcategory Subcategory { get; set; }
}
Any idea how to solve it?
Sequence contains more than one element
This exception was caused when trying to use
AddOrUpdate
with specifying identifier expression, likep => p.CategoryName
. There might be twoCategories
that have the same name "Sport" or "Music".This might also happen on
Subcategories
andServices
,Subcategories
usesp => p.SubcategoryName
andServices
usesp => p.ServiceType
.You need to clean the duplicate entry first on database. This is a must, since you want to use
AddOrUpdate
, internally this code will useSingleOrDefault
and if there is found more than one match element, exception will be thrown.Object reference not set to an instance of an object
This error is probably caused by this code.
Creating new
Service
will have nullCategory
by default, you can't just directly set theCategoryName
.The problem occurs with when I add new Services. I want to use the already added categories and subcategories.
The
Categories
andSubcategories
should be stored on variables so it can be used byService
.But above code still has problem, if service is new entity, the existing sport category and the existing football subcategory will also be added. You can read this article for further explanation.
Solution
You need to also have foreign key values on
Service
not only foreign key references to prevent adding existing category and existing subcategory.Then run the migration.
And assign temporary primary key manually and use it when defining service. This temporary primary keys are not needed when dealing with existing entities, but there might be another problem if there is more than one new category or subcategory. To prevent that, it's better to use temporary primary keys, after calling
AddOrUpdate
, each entity primary keys will be updated if they are existing entities.Then update the database.