简体   繁体   English

实体框架-一次插入多个复杂对象

[英]Entity Framework - Insert multiple complex objects at once

I am writing a parser for one of website, which have products connected to categories. 我正在为网站之一编写解析器,该网站具有与类别相关的产品。 I am trying to build my own database with these items. 我正在尝试使用这些项目构建自己的数据库。

I have decided to use Entity Framework, but I am new to this framework, so here's my problem: 我决定使用实体框架,但是我对这个框架不熟悉,所以这是我的问题:

During parsing i have multiple items with same category. 在解析期间,我有多个具有相同类别的项目。 But categories are kind of trees. 但是类别是种树木。 I mean, category have a reference to parentCategory. 我的意思是,类别有对parentCategory的引用。

During of parsing i have a list of category inheritance fe : category1 -> category1.1 -> category1.1.1 在解析期间,我有一个类别继承列表fe:category1-> category1.1-> category1.1.1

Each product I parse and add to database need to verify if that category exist and go through category inheritance to create non existing categories. 我解析并添加到数据库的每个产品都需要验证该类别是否存在,并通过类别继承来创建不存在的类别。

Code looks like this: 代码如下:

Category parentCategory = null;
foreach (var sCategory in categories)
{
    var currentCategory = d.CategorySet.SingleOrDefault(category => category.Name == sCategory && category.Parent == parentCategory);
    if (currentCategory == null)
    {
        currentCategory = new Category(){Name = sCategory,Parent = parentCategory};
        if(parentCategory != null)
            d.Entry(parentCategory).State = EntityState.Unchanged;
    }
    parentCategory = currentCategory;
}

But in this case, SingleOrDefault LinQ does not work because of exception: 但是在这种情况下,由于异常,SingleOrDefault LinQ无法正常工作:

Unable to create a constant value of type 'DataBaseModel.Category'. Only primitive types or enumeration types are supported in this context.

I know that I should compare IDs of category, but in this case it needs to saveChanges into db every time I add sth to DB. 我知道我应该比较类别的ID,但是在这种情况下,每次我将某项添加到DB时,它都需要将ChangeSaves保存到db中。

Is there any other possibility to handle that? 还有其他可能性可以解决吗?

I have solved this issue by creating local Dictionaries of Categories and before usage, fill this dictionaries by data from database. 我已经通过创建本地分类词典来解决此问题,并且在使用之前,请使用数据库中的数据填充此词典。

_categoriesDictionary.Clear();
foreach (var category in this.Container.CategorySet)
{
    Category temp = category;
    string fullCategoryString = "";
    while (temp != null)
    {
        fullCategoryString = fullCategoryString.Insert(0, temp.Name + ";");
        temp = temp.Parent;
    }
    _categoriesDictionary.Add(fullCategoryString, category);
}

And then when analyzing the record: 然后在分析记录时:

Category parentCategory = null;
string fullCatString = "";
foreach (var sCategory in categories)
{
    fullCatString += sCategory + ";";
    Category currentCategory;
    if (!_categoriesDictionary.TryGetValue(fullCatString, out currentCategory))
    {

        currentCategory = new Category()
        {
            Name = sCategory,
            Parent = parentCategory
        };
        this.Container.CategorySet.Add(currentCategory);
        _categoriesDictionary.Add(fullCatString, currentCategory);
    }
    parentCategory = currentCategory;
}
result.Category = parentCategory;

This has another adventage from my point of view: Its collecting data on start, and then do not query DB every time 从我的角度来看,这还有另一个问题:它从开始收集数据,然后每次都不查询数据库

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM