Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在0到多个子表中编辑(添加/删除)行的最佳方法_C#_Asp.net Mvc_Entity Framework_Ef Code First - Fatal编程技术网

C# 在0到多个子表中编辑(添加/删除)行的最佳方法

C# 在0到多个子表中编辑(添加/删除)行的最佳方法,c#,asp.net-mvc,entity-framework,ef-code-first,C#,Asp.net Mvc,Entity Framework,Ef Code First,我首先使用EF6.1代码和MVC5 我有一个父表,子表中有0到多个子表 下面是我的父/子模型我的视图模型,以及编辑的控制器中的代码 下面我所做的是确保它从子表中删除已删除的行,添加新行,并保留未单独更改的现有行 然而,我想知道是否有更好、更简单的方法来处理这种情况 public class Parent { [Key] [Required] public int ParentId { get; set; } public virtual ICollection&

我首先使用EF6.1代码和MVC5

我有一个父表,子表中有0到多个子表

下面是我的父/子模型我的视图模型,以及编辑的控制器中的代码

下面我所做的是确保它从子表中删除已删除的行,添加新行,并保留未单独更改的现有行

然而,我想知道是否有更好、更简单的方法来处理这种情况

public class Parent
{
    [Key]
    [Required]
    public int ParentId { get; set; }

    public virtual ICollection<Child> Children { get; set; }
}

public class Child
{
    [Key]
    [Required]
    public int ChildId { get; set; }

    public int ParentId { get; set; }

    public virtual Parent Parent { get; set; }

}

public class ParentViewModel
{
    public ChildrenViewModel Children { get; set; }
}

public class ChildrenViewModel 
{
    public int[] SelectedValues { get; set; } // The Selected Values
    public IEnumerable<SelectListItem> Items { get; set; } // Items in the List Box
}

if (parentViewModel.Children == null)
{
    // If the collection in the parent view model is null, then clear the collection in the parent and save
    parent.Children.Clear();
}
else
{
    // add new items if it exists in the view model but isnt already in the database
        foreach (int childId in parentViewModel.Children.SelectedValues)
        {
            if (!parent.Children.Any(f => f.ChildId == childId))
            {
                offense.Children.Add(new Child{ ChildId = childId });
            }
        }

        // delete children from the parent that are no longer present in the view model collection

        IList<Child> deleteList = new List<Child>();
        foreach (Child child in parent.Children)
        {
            if (!parentViewModel.Children.SelectedValues.Contains(child.ChildId))
            {
                deleteList.Add(child);
            }
        }

        foreach (Child child in deleteList)
        {
            parent.Children.Remove(child);
        }
    }
db.Save()
公共类父类
{
[关键]
[必需]
public int ParentId{get;set;}
公共虚拟ICollection子项{get;set;}
}
公营儿童
{
[关键]
[必需]
public int ChildId{get;set;}
public int ParentId{get;set;}
公共虚拟父级{get;set;}
}
公共类ParentViewModel
{
public ChildrenViewModel子项{get;set;}
}
公共类儿童视图模型
{
public int[]SelectedValues{get;set;}//所选值
公共IEnumerable项{get;set;}//列表框中的项
}
if(parentViewModel.Children==null)
{
//如果父视图模型中的集合为null,则清除父视图模型中的集合并保存
parent.Children.Clear();
}
其他的
{
//如果视图模型中存在但数据库中不存在新项,则添加新项
foreach(parentViewModel.Children.SelectedValues中的int childId)
{
如果(!parent.Children.Any(f=>f.ChildId==ChildId))
{
Add(new Child{ChildId=ChildId});
}
}
//从父对象中删除视图模型集合中不再存在的子对象
IList deleteList=新列表();
foreach(parent.Children中的子项)
{
如果(!parentViewModel.Children.SelectedValues.Contains(child.ChildId))
{
deleteList.Add(子项);
}
}
foreach(删除列表中的子项)
{
父。子。移除(子);
}
}
db.Save()

可能有一个“Tryhard”解决方案,但我认为这看起来不错。我会这样做的。这会导致性能问题吗?不会,不会导致性能问题。我是实体框架和MVC的新手,希望确保我没有做傻事。非常感谢。