简体   繁体   中英

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.

During of parsing i have a list of category inheritance 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:

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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