首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用子实体使用efcore.bulkextensions的BulkInsert

使用子实体使用efcore.bulkextensions的BulkInsert
EN

Stack Overflow用户
提问于 2021-01-28 21:44:47
回答 1查看 39关注 0票数 0

我编写了以下代码来插入记录Product basic信息和属性:

代码语言:javascript
复制
foreach (var item in records)
                {

                    var productList = new ProductsList()
                    {
                        CreatedDate = DateTime.Now,
                        ItemGroup = item.ItemGroup,
                        ItemType = item.itemType,
                        LegalEntitiesId = legalEntityId,
                        PostedToOtherSys = false,
                        ProductName = item.prodName,
                        ProductNumber = item.prodid,
                        UOM = item.UOM,
                    };

                    foreach (var attribute in item.attributes)
                    {
                        var productListAttributeValues = new ProductListAttributeValues()
                        {
                            CreatedDate = DateTime.Now,
                            Name = attribute.Name,
                            Value = attribute.type
                        };
                        productList.ProductListAttributeValues.Add(productListAttributeValues);
                    }

                    _unitOfWork.productsListRepository.Add(productList);
                    _unitOfWork.Save();
                }
            }

我正在使用efcore.bulkextensions库,但我不知道如何在基于文档的外键关系存在时插入基实体和子实体:

Documentaion

我试着这样做:

代码语言:javascript
复制
_unitOfWork.productsListRepository.BulkAdd(listOfProducts);
                
                foreach (var entity in listOfProducts)
                {
                    foreach (var subEntity in entity.ProductListAttributeValues)
                    {
                        subEntity.ProductsListId = entity.Id; // setting FK to match its linked PK that was generated in DB
                        listOfProductsAttributes.Add(subEntity);
                    }
                    _unitOfWork.productListAttributeValuesRepository.AddRange(entity.ProductListAttributeValues.ToList());
                }
                _unitOfWork.productListAttributeValuesRepository.BulkAdd(listOfProductsAttributes);
EN

回答 1

Stack Overflow用户

发布于 2021-05-27 22:16:32

您必须将BulkConfig选项添加到底层dbContext.BulkInsert调用中,如下所示:

_context.BulkInsert(, new BulkConfig { IncludeGraph = true });

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65938327

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档