我创建了一些模型,添加了迁移,然后执行了更新数据库操作,但在我最后一次更新数据库操作时,我收到了错误消息:
Sequence contains more than one element
您可以在下面找到我的迁移配置:
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" }
}
);
问题发生在我添加新服务时。我已经有了类别和子类别,如果我喜欢 Category = new Category { CategoryName = "Music"}
那么它就可以工作,但我在我的数据库中获得了两次音乐条目(对于这个例子)。我想使用已经添加的类别和子类别。您还可以在下面找到我的模型定义。
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; }
}
知道怎么解决吗?
最佳答案
序列包含多个元素
此异常是在尝试使用 AddOrUpdate
时引起的指定标识符表达式,如 p => p.CategoryName
.可能有两个 Categories
具有相同名称“运动”或“音乐”。
这也可能发生在 Subcategories
上和 Services
, Subcategories
使用 p => p.SubcategoryName
和 Services
使用 p => p.ServiceType
.
您需要先清除数据库中的重复条目。这是必须的,因为你想使用 AddOrUpdate
, 在内部这段代码将使用 SingleOrDefault
如果找到多个匹配元素,将抛出异常。
对象引用未设置到对象的实例
这个错误可能是由这段代码引起的。
Category = { CategoryName = "Sport" },
Subcategory = { SubcategoryName = "Football" }
正在创建新的 Service
将有空 Category
默认情况下,您不能直接设置 CategoryName
.
问题出现在我添加新服务时。我想使用已经添加的类别和子类别。
Categories
和 Subcategories
应该存储在变量上,以便 Service
可以使用它.
var sportCategory = new Category { CategoryName = "Sport" };
var musicCategory = new Category { CategoryName = "Music" };
context.Categories.AddOrUpdate(p => p.CategoryName,
sportCategory, musicCategory);
var footballSub = new Subcategory { SubcategoryName = "Football" };
var basketballSub = new Subcategory { SubcategoryName = "Basketball" };
var pianoSub = new Subcategory { SubcategoryName = "Piano" };
var violinSub = new Subcategory { SubcategoryName = "Violin" };
context.Subcategories.AddOrUpdate(p => p.SubcategoryName,
footbalSub, basketballSub , pianoSub, violinSub);
context.Services.AddOrUpdate(p => p.ServiceType,
new Service
{
ServiceType = "Football player",
Category = sportCategory,
Subcategory = footballSub
},
new Service
{
ServiceType = "Piano lessons",
Category = musicCategory,
Subcategory = pianoSub
}
);
但是上面的代码还是有问题,如果service是new entity,那么现有的sport类和football子类也会被添加进来。你可以阅读this article进一步解释。
解决方案
您还需要在 Service
上有外键值不仅要防止添加现有类别和现有子类别的外键引用。
[ForeignKey("Category")]
public int CategoryId { get; set; }
[ForeignKey("Subcategory")]
public int SubcategoryId { get; set; }
然后运行迁移。
Add-Migration AddServiceFKValues
并分配temporary primary key手动并在定义服务时使用它。在处理现有实体时不需要此临时主键,但如果有多个新类别或子类别,则可能会出现另一个问题。为防止这种情况,最好在调用 AddOrUpdate
后使用临时主键。 ,如果每个实体的主键是现有实体,它们将被更新。
var sportCategory = new Category { CategoryID = 1000, CategoryName = "Sport" };
var musicCategory = new Category { CategoryID = 1001, CategoryName = "Music" };
context.Categories.AddOrUpdate(p => p.CategoryName,
sportCategory, musicCategory);
var footballSub = new Subcategory { SubcategoryID = 1000, SubcategoryName = "Football" };
var basketballSub = new Subcategory { SubcategoryID = 1001, SubcategoryName = "Basketball" };
var pianoSub = new Subcategory { SubcategoryID = 1002, SubcategoryName = "Piano" };
var violinSub = new Subcategory { SubcategoryID = 1003, SubcategoryName = "Violin" };
context.Subcategories.AddOrUpdate(p => p.SubcategoryName,
footbalSub, basketballSub , pianoSub, violinSub);
context.Services.AddOrUpdate(p => p.ServiceType,
new Service
{
ServiceType = "Football player",
CategoryID = sportCategory.CategoryID,
SubcategoryID = footballSub.SubcategoryID
},
new Service
{
ServiceType = "Piano lessons",
CategoryID = musicCategory.CategoryID,
SubcategoryID = pianoSub.SubcategoryID
}
);
然后更新数据库。
Update-Database
关于c# - 序列在 EF CF 迁移中包含多个元素错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25233769/