C
C#3y ago
Kamil Pisz

[EFCore] add many Parent entities with Child entities, How to connect them in single SaveChanges

Hello, im trying to sync data in my database with data in external source there is a example that i have list of MainCategories (parent) and Categories(child) !! code screen from IDE for better visibility in comments
var listOfParentCategoriesFromExternalSource;

var newListOfParentCategoryToAdd = new List<ParentCategory>();
foreach(parent in listOfParentCategoriesFromExternalSource)
{
var newParentCategory = ParentCategory();
newParentCategory.Name = "Main Category Name";

var newListChildCategoryToAdd = new List<ChildCategory>();
foreach(child in parent)
{
var newChildCategory = ChildCategory();
newChildCategory.Name ="Child Category Name"
newChildCategory.ParentId = ?? // < --------- How to make connection ?

newListChildCategoryToAdd.Add(newChildCategory) //----Prepare list to AddRange

}
newListOfParentCategoryToAdd.Add(newParentCategory) //----Prepare list to AddRange
}
await _dbContext.ParentsCategory.AddRangeAsync(newListOfParentCategoryToAdd);
await _dbContext.ParentsCategory.AddRangeAsync(newListChildCategoryToAdd);
await _dbContext.SaveChangesAsync();
var listOfParentCategoriesFromExternalSource;

var newListOfParentCategoryToAdd = new List<ParentCategory>();
foreach(parent in listOfParentCategoriesFromExternalSource)
{
var newParentCategory = ParentCategory();
newParentCategory.Name = "Main Category Name";

var newListChildCategoryToAdd = new List<ChildCategory>();
foreach(child in parent)
{
var newChildCategory = ChildCategory();
newChildCategory.Name ="Child Category Name"
newChildCategory.ParentId = ?? // < --------- How to make connection ?

newListChildCategoryToAdd.Add(newChildCategory) //----Prepare list to AddRange

}
newListOfParentCategoryToAdd.Add(newParentCategory) //----Prepare list to AddRange
}
await _dbContext.ParentsCategory.AddRangeAsync(newListOfParentCategoryToAdd);
await _dbContext.ParentsCategory.AddRangeAsync(newListChildCategoryToAdd);
await _dbContext.SaveChangesAsync();
3 Replies
Kamil Pisz
Kamil PiszOP3y ago
so there is other way, i can just call SaveChanges for ever iteration in my Foreach and them withdraw new Entity ID that new Entity Id will be my newChildCategory.ParentId but due to frequency of that function will be use i want to optimize it to less SaveChanges()
Kamil Pisz
Kamil PiszOP3y ago
-- Screen from IDE for better visibility
Kamil Pisz
Kamil PiszOP3y ago
UPDATE: so i found a way to add list of child entities to single parent category using Navigation Property in Parent Entity
public virtual ICollection<ChildCategory> ChildCategories { get; set; } = new List<ChildCategory>();
public virtual ICollection<ChildCategory> ChildCategories { get; set; } = new List<ChildCategory>();
newParentCategory.ChildCategories = newListChildCategoryToAdd;
newParentCategory.ChildCategories = newListChildCategoryToAdd;
but not sure if i have to use AddRange() for children list now
Want results from more Discord servers?
Add your server