c# - 多线程 EF6 中的主键冲突

标签 c# multithreading entity-framework ef-code-first primary-key

我正在开发一个 C# 控制台应用程序,该应用程序从激战 2 API 下载数据并使用 Entity Framework 6 将其输入到我的数据库中。我正在尝试使用多线程,以便我可以加快输入过程大量数据进入我的数据库。

问题是当代码运行到我的 AddRecipes 方法中的 DBContext.SaveChanges() 调用时,返回以下错误:

Violation of PRIMARY KEY constraint 'PK_dbo.Items'. Cannot insert duplicate key in object 'dbo.Items'. The duplicate key value is (0).

这是与我的问题相关的代码部分:

class Program
{
    private static ManualResetEvent resetEvent;
    private static int nIncompleteThreads = 0;

    //Call this function to add to the dbo.Items table
    private static void AddItems(object response)
    {
        string strResponse = (string)response;

        using (GWDBContext ctx = new GWDBContext())
        {
            IEnumerable<Items> itemResponse = JsonConvert.DeserializeObject<IEnumerable<Items>>(strResponse);

            ctx.Items.AddRange(itemResponse);
            ctx.SaveChanges();
        }

        if (Interlocked.Decrement(ref nIncompleteThreads) == 0)
        {
            resetEvent.Set();
        }
    }

    //Call this function to add to the dbo.Recipes table
    private static void AddRecipes(object response)
    {
        string strResponse = (string)response;

        using (GWDBContext ctx = new GWDBContext())
        {
            IEnumerable<Recipes> recipeResponse = JsonConvert.DeserializeObject<IEnumerable<Recipes>>(strResponse);

            ctx.Recipes.AddRange(recipeResponse);

            foreach(Recipes recipe in recipeResponse)
            {
                ctx.Ingredients.AddRange(recipe.ingredients);
            }
            ctx.SaveChanges(); //This is where the error is thrown
        }

        if (Interlocked.Decrement(ref nIncompleteThreads) == 0)
        {
            resetEvent.Set();
        }
    }

    static void GetResponse(string strLink, string type)
    {
        //This method calls the GW2 API through HTTPWebRequest
        //and store the responses in a List<string> responseList variable.
        GWHelper.GetAllResponses(strLink);

        resetEvent = new ManualResetEvent(false);
        nIncompleteThreads = GWHelper.responseList.Count();

        //ThreadPool.QueueUserWorkItem creates threads for multi-threading
        switch (type)
        {
            case "I":
                {
                    foreach (string strResponse in GWHelper.responseList)
                    {
                        ThreadPool.QueueUserWorkItem(new WaitCallback(AddItems), strResponse);
                    }
                    break;
                }
            case "R":
                {
                    foreach (string strResponse in GWHelper.responseList)
                    {
                        ThreadPool.QueueUserWorkItem(new WaitCallback(AddRecipes), strResponse);
                    }
                    break;
                }
        }

        //Waiting then resetting event and clearing the responseList
        resetEvent.WaitOne();           
        GWHelper.responseList.Clear();
        resetEvent.Dispose();
    }

    static void Main(string[] args)
    {
        string strItemsLink = "items";
        string strRecipesLink = "recipes";

        GetResponse(strItemsLink, "I");
        GetResponse(strRecipesLink, "R");

        Console.WriteLine("Press any key to continue...");
        Console.ReadLine();
    }

这是我的 DBContext 类:

public class GWDBContext : DbContext
{
    public GWDBContext() : base("name=XenoGWDBConnectionString") { }

    public DbSet<Items> Items { get; set; }
    public DbSet<Recipes> Recipes { get; set; }
    public DbSet<Ingredient> Ingredients { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
    }
}

这也是我的表类(我知道名称很困惑,我正在努力重写它们):

public class Items
{
    public Items()
    {
        Recipes = new HashSet<Recipes>();
        Ingredients = new HashSet<Ingredient>();
    }

    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.None)] //This attribute makes sure that the id column is not an identity column since the api is sending that).
    public int id { get; set; }

    .../...
    public virtual ICollection<Recipes> Recipes { get; set; }
    public virtual ICollection<Ingredient> Ingredients { get; set; }
}

public class Recipes
{
    public Recipes()
    {
        disciplines = new List<string>();
        ingredients = new HashSet<Ingredient>();
    }

    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.None)] //This attribute makes sure that the id column is not an identity column since the api is sending that).
    public int id { get; set; }
    public string type { get; set; }

    [ForeignKey("Items")] //This attribute points the output_item_id column to the Items table.

    .../...
    private List<string> _disciplines { get; set; }
    public List<string> disciplines
    {
        get { return _disciplines; }
        set { _disciplines = value; }
    }

    [Required]
    public string DisciplineAsString
    {
        //get; set;
        get { return string.Join(",", _disciplines); }
        set { _disciplines = value.Split(',').ToList(); }
    }

    public string chat_link { get; set; }

    public virtual ICollection<Ingredient> ingredients { get; set; }
    public virtual Items Items { get; set; }
}

public class Ingredient
{
    public Ingredient()
    {
        Recipe = new HashSet<Recipes>();
    }

    [Key]
    public int ingredientID { get; set; }

    [ForeignKey("Items")] //This attribute points the item_id column to the Items table.
    public int item_id { get; set; }
    public int count { get; set; }

    public virtual ICollection<Recipes> Recipe { get; set; }
    public virtual Items Items { get; set; }
}

以下链接解释了 Items/Recipes 类返回的内容:

Items

Recipes

我注意到在删除外键约束和 public virtual Items Items { get;放; } 代码数据将被正确保存。我相信我的错误与 Recipes 类中的 public virtual Items Items 有关。但是根据我的理解,我需要在类中有那个虚拟变量,这样 Entity Framework 才能知道类之间的关系。那么,为什么在我的类中使用该虚拟变量会导致抛出主键违规?

最佳答案

您只需要食谱中的项目列表。如果您需要搜索哪些食谱有特定项目,您可以通过搜索 Recipe 的外键(Item 的主键)来实现。

代码存在一个基本的命名缺陷。您有一个 Recipes 类,然后是一个名为 Recipes 的食谱列表。与项目相同。

然后您有一个用于 Recipes 的外键 [ForeignKey("Items")]。这是哪些项目?列表或对象项。它很容易出错。

将您的类重命名为RecipeItem

public class Recipe
{ 
    public Recipe()


public class Item
{
    public Item()

此外 - 如评论中所述,0 的重复 Id 听起来好像没有设置 Id

查看食谱链接:

{
    .../...
    "ingredients": [
            { "item_id": 19684, "count": 50 },
            { "item_id": 19721, "count": 1 },
            { "item_id": 46747, "count": 10 }
    ],
    "id": 7319,
    .../...
}

食谱不应包含项目列表,而应包含配料列表。类结构应该是:

Recipe has:
public virtual ICollection<Ingredient> Ingredients { get; set; }

Ingredient has:
public virtual ICollection<Item> Items { get; set; }

Item 类没有 Ingredients 或 Recipes 列表,这些列表是通过使用与 Item 的主键匹配的外键查询 Ingredient 上的数据库 Items 来检索的,或者食谱外键上的数据库成分与成分的主键匹配 - 然后您可以进行连接以查找这些成分的任何项目。

所以做如下修改:

无需在 Item 类中提及 Recipe 或 Ingredients。

public Item() // remove pluralisation
{
    // Remove these from the constructor,
    // Recipes = new HashSet<Recipes>();
    // Ingredients = new HashSet<Ingredient>();
}

// remove these from the class.
// public virtual ICollection<Recipes> Recipes { get; set; }
// public virtual ICollection<Ingredient> Ingredients { get; set; }

一种成分有很多元素 - 因此是元素的集合

public Ingredient()
{
    // You don't need a collection of Recipes - 
    // you need a collection of Items.
    // Recipe = new HashSet<Recipes>();
}
.../...
[ForeignKey("Item")] // change this
public Item Item // include an Item object - the PK of the
    // Item is the FK of the Ingredient


.../...
// Remove Recipes
// public virtual ICollection<Recipes> Recipe { get; set; }
public virtual ICollection<Item> Items { get; set; }

我更喜欢 using the object name for the variable Item Item .

一个食谱有很多配料 - 因此是配料的集合

public Recipes()
{
    disciplines = new List<string>();
    ingredients = new HashSet<Ingredient>();
}
.../...
// [ForeignKey("Items")] remove this
[ForeignKey("Ingredient")] 
public Ingredient Ingredient // include as Ingredient object 
    // the PK of the Ingredient is the FK for the Recipe

.../...
public virtual ICollection<Ingredient> Ingredients { get; set; }
// The Recipe does not have an Item, the Ingredient has 
// has a collection of <Item> Items
// public virtual Items Items { get; set; }

此外,我不确定您为什么要使用哈希集。如果您没有特别的理由使用它们,我会将它们制作成列表。

如果这不能修复您的代码,我将检查它的其余部分。

关于c# - 多线程 EF6 中的主键冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45260221/

相关文章:

c# - 如何获取引用变量的地址?

c# - 使用 foreach 获取文本框值

java - Java ForkJoinPool 中的某些线程偶尔会空闲

c# - 多线程环境中的任务列表 - "enumeration operation may not execute"

c# - SET IDENTITY_INSERT [表] ON 不起作用

c# - 在VB.net/C#中直接使用资源字体

c# - 为所有实体设置级联删除

c# - 迁移期间未找到 Entity Framework 外键

Javafx 程序卡住

c# - EF Core 2 - 只有 1 个导航属性/关系知识的关系?