多线程下的EF6主键冲突问题

3
我正在开发一个C#控制台应用程序,从公会战争2 API下载数据,并使用Entity Framework 6将其输入到我的数据库中。我试图使用多线程来加速将大量数据输入到我的数据库的过程。
问题在于,当代码运行到我的AddRecipes方法中的DBContext.SaveChanges()调用时,会返回以下错误:
违反了“PK_dbo.Items”主键约束。无法向对象“dbo.Items”中插入重复键值(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; set; }代码后,数据将保存正确。我认为我的错误与在Recipes类中拥有public virtual Items Items有关。但从我的理解来看,我需要在类中拥有虚拟变量,以便Entity Framework知道类之间的关系。那么,为什么在我的类中拥有该虚拟变量会导致主键冲突?

食谱上的物品是否正确?我只是想知道您是否可能有一些带有0个ID或其他问题的物品... - Chris
它正在返回重复值,你能检查一下吗? - DavidG
我移除了 'DatabaseGeneratedOption.None' 这个选项,现在它可以工作了。但是现在的 ID 不同于 GW2 API 的 ID。有没有什么解决方法?如果没有,那么我可能需要创建两个 ID 列。 - John Odom
1
类和链接的JSON对象之间存在相当大的不匹配。 - Gert Arnold
1
我认为这只是由于在插入配料时重新插入现有的Items对象所导致的,但是我需要查看recipeResponse的确切内容才能验证这一点。 - Gert Arnold
显示剩余9条评论
1个回答

7
你只需要食谱中的项目列表。如果你需要搜索拥有特定项目的食谱,你可以通过在配方(Item的主键)的外键上搜索来实现。
代码存在一个基本的命名缺陷。你有一个类 Recipes,然后是一个名为 Recipes 的 Recipes 列表。Items也是一样。
然后你有一个针对 Recipes 的外键 [ForeignKey("Items")]。这个Items是什么?List还是Object Items?它很容易出错。
将你的类重命名为 Recipe Item
public class Recipe
{ 
    public Recipe()


public class Item
{
    public Item()

此外,根据评论中提到的重复的Id0,看起来Id没有被设置。
查看Recipes链接:
{
    .../...
    "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”类没有配料或食谱列表,这些列表是通过查询具有与“Item”的主键匹配的外键的配料数据库上的“项目”,或者查询具有与配料的主键匹配的食谱外键的“配料”数据库上的 - 然后可以进行连接以查找这些配料的任何项目。

因此,请进行以下更改:

“Item”类中不需要提到配方或配料。

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

I prefer 使用对象名称作为变量项食谱有许多成分 - 因此是成分的集合
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; }

我不确定你为什么要使用Hashsets。如果没有特殊原因,我会将它们转换为Lists。

如果这不能解决你的问题,我会检查剩下的代码。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接