序列包含EF CF迁移多个元素错误 [英] Sequence contains more than one element error in EF CF Migrations

查看:164
本文介绍了序列包含EF CF迁移多个元素错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一些车型,增加了迁移,然后做了一个更新的数据库操作,但在我的最后一次更新数据库的操作,我得到了错误信息说:

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:

序列包含多个元素

下面你可以找到我的迁移配置:

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?

推荐答案

序列包含多个元素

这异常被引发时,试图用 AddOrUpdate 与指定的标识符的表情,就像 p => p.CategoryName 。可能有两个类别具有相同的名字运动或音乐。

This exception was caused when trying to use AddOrUpdate with specifying identifier expression, like p => p.CategoryName. There might be two Categories that have the same name "Sport" or "Music".

这也可能发生在小类服务小类使用 p => p.SubcategoryName 服务使用 P => p.ServiceType

This might also happen on Subcategories and Services, Subcategories uses p => p.SubcategoryName and Services uses p => p.ServiceType.

您需要先清理数据库重复条目。这是必须的,因为你要使用 AddOrUpdate ,内部的代码将使用的SingleOrDefault ,如果有发现更多不止一个匹配的元素,会抛出异常。

You need to clean the duplicate entry first on database. This is a must, since you want to use AddOrUpdate, internally this code will use SingleOrDefault and if there is found more than one match element, exception will be thrown.

对象引用不设置到对象的实例

此错误可能是由该代码引起的。

This error is probably caused by this code.

Category = { CategoryName = "Sport" },
Subcategory = { SubcategoryName = "Football" }

创建新的服务将空类别默认情况下,你不能只是直接设置类别名称

Creating new Service will have null Category by default, you can't just directly set the CategoryName.

与当我添加新的服务出现问题。我想使用已添加类别和子类别。

类别小类应存放在变量,以便它可以通过服务中。

The Categories and Subcategories should be stored on variables so it can be used by 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
    }
);



不过,上面的代码还是有问题的,如果服务是新的实体,现有的体育类别和现有的足球子类别也将添加。您可以阅读作进一步的解释这篇文章

解决方案

您也需要对服务外键值不仅外键引用,避免增加现有类别和现有的子类别。

You need to also have foreign key values on Service not only foreign key references to prevent adding existing category and existing subcategory.

[ForeignKey("Category")]
public int CategoryId { get; set; }
[ForeignKey("Subcategory")]
public int SubcategoryId { get; set; }



然后运行迁移。

Then run the migration.

Add-Migration AddServiceFKValues

和指定的临时主键手动和定义服务时使用它。与现有的实体处理时不需要此临时主密钥,但是如果有一个以上的新的类别或子类别有可能是另一个问题。为了防止这种情况,最好是使用临时主键,调用后 AddOrUpdate ,每个实体主键会,如果他们现有的实体进行更新。

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.

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



然后更新数据库。

Then update the database.

Update-Database

这篇关于序列包含EF CF迁移多个元素错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆