zoukankan      html  css  js  c++  java
  • EF

    public void UpdateEntity(ParentItem parent)
    {
        // Load original parent including the child item collection
        var originalParent = _dbContext.ParentItems
            .Where(p => p.ID == parent.ID)
            .Include(p => p.ChildItems)
            .SingleOrDefault();
        // We assume that the parent is still in the DB and don't check for null
    
        // Update scalar properties of parent,
        // can be omitted if we don't expect changes of the scalar properties
        var parentEntry = _dbContext.Entry(originalParent);
        parentEntry.CurrentValues.SetValues(parent);
    
        foreach (var childItem in parent.ChildItems)
        {
            var originalChildItem = originalParent.ChildItems
                .Where(c => c.ID == childItem.ID && c.ID != 0)
                .SingleOrDefault();
            // Is original child item with same ID in DB?
            if (originalChildItem != null)
            {
                // Yes -> Update scalar properties of child item
                var childEntry = _dbContext.Entry(originalChildItem);
                childEntry.CurrentValues.SetValues(childItem);
            }
            else
            {
                // No -> It's a new child item -> Insert
                childItem.ID = 0;
                originalParent.ChildItems.Add(childItem);
            }
        }
    
        // Don't consider the child items we have just added above.
        // (We need to make a copy of the list by using .ToList() because
        // _dbContext.ChildItems.Remove in this loop does not only delete
        // from the context but also from the child collection. Without making
        // the copy we would modify the collection we are just interating
        // through - which is forbidden and would lead to an exception.)
        foreach (var originalChildItem in
                     originalParent.ChildItems.Where(c => c.ID != 0).ToList())
        {
            // Are there child items in the DB which are NOT in the
            // new child item collection anymore?
            if (!parent.ChildItems.Any(c => c.ID == originalChildItem.ID))
                // Yes -> It's a deleted child item -> Delete
                _dbContext.ChildItems.Remove(originalChildItem);
        }
    
        _dbContext.SaveChanges();
    }
    
    
    //---------------------------------------------------
    //---------------------------------------------------
    
    var parent = context.Parents.GetById(1); // Make sure that childs are loaded as well
    foreach(var child in modifiedParent.ChildItems)
    {
        var attachedChild = FindChild(parent, child.Id);
        if (attachedChild != null)
        {
            // Existing child - apply new values
            context.Entry(attachedChild).CurrentValues.SetValues(child);
        }
        else
        {
            // New child
            // Don't insert original object. It will attach whole detached graph
            parent.ChildItems.Add(child.Clone());
        }
    }
    
    // Now you must delete all entities present in parent.ChildItems but missing
    // in modifiedParent.ChildItems
    // ToList should make copy of the collection because we can't modify collection
    // iterated by foreach
    foreach(var child in parent.ChildItems.ToList())
    {
        var detachedChild = FindChild(modifiedParent, child.Id);
        if (detachedChild == null)
        {
            parent.ChildItems.Remove(child);
            context.Childs.Remove(child); 
        }
    }
    
    context.SaveChanges();
    

      

  • 相关阅读:
    Dreamweaver中SourceAnyWhere的使用
    访问被拒绝:“AjaxPro”的解决方案
    NDoc1.3.1使用手册
    程序中操作Excel
    模拟提交程序相关专题
    利用SMTP服务发送电子邮件
    如何使用树形控件
    GDI+简单使用例子
    程序中操作Word
    签名工具使用介绍
  • 原文地址:https://www.cnblogs.com/luchaoshuai/p/5486501.html
Copyright © 2011-2022 走看看