多线程 EF6 中的主键冲突

Primary Key Violation in Multi-threading EF6

我正在开发一个 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 class:

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

这也是我的 table classes(我知道这些名字很混乱,我正在努力重写它们):

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 class 返回的内容:

Items

Recipes

我注意到在删除外键约束和 public virtual Items Items { get; set; } 代码后,数据将被正确保存。我相信我的错误与食谱 class 中的 public virtual Items Items 有关。但根据我的理解,我需要在 class 中包含该虚拟变量,以便 Entity Framework 可以知道 class 之间的关系。那么,为什么在我的 class 中使用该虚拟变量会导致抛出主键违规?

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

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

然后您就有了 Recipes 的外键 [ForeignKey("Items")]。这是哪些项目?列表或对象项。太容易出错了。

将您的 class 重命名为 RecipeItem

public class Recipe
{ 
    public Recipe()


public class Item
{
    public Item()

另外 - 正如评论中提到的 0 的重复 Id,听起来 Id 没有被设置。

查看 link 食谱:

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

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

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

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

Item class 没有配料或食谱列表,这些列表是通过查询数据库中配料上的项目来检索的,外键与项目的主键匹配,或与配料主键匹配的配方外键上的数据库配料 - 然后您可以进行连接以查找这些配料的任何项目。

因此进行以下更改:

无需在项目中提及食谱或成分 class。

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

我更喜欢.

一个食谱有很多配料 - 因此有一组配料

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

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

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