繁体   English   中英

如何使用实体框架在数据库中保存多条主细节记录

[英]How to save multiple records of master detail in database using Entity Framework

我不想在数据库中保存多个子记录,但我的代码只输入一条记录我的代码如下

foreach (var existingChild in fellowClass.Fellow_Characters.ToList())
{   
    if (!fellowMasterEntity.Fellow_Characters.Any(c => c.FellowCharacterID == existingChild.FellowCharacterID))
        fellowCharacterRepository.Delete(existingChild);
}

foreach (var childModel in fellowMasterEntity.Fellow_Characters)
{
    var existingChild = fellowClass.Fellow_Characters
                                   .Where(c => c.FellowCharacterID == childModel.FellowCharacterID)
                                   .SingleOrDefault();

    if (existingChild != null)
    {
        _context.Entry(existingChild).CurrentValues.SetValues(childModel);
    }
    else
    {
        var childEntity = new Fellow_Characters
                        {
                            //FellowCharacterID = childModel.FellowCharacterID,
                            FellowID = childModel.FellowID,
                            CharacterID = childModel.CharacterID
                        };

        fellowCharacterRepository.Insert(childEntity);
        //existingChild.Fellow.Fellow_Characters.Add(childEntity);
    }
}

如何保存多个子记录?

暂无
暂无

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

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