Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.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# 在处理集合时,EF 6中处理CRUD操作的正确方法是什么?_C#_Entity Framework_Crud - Fatal编程技术网

C# 在处理集合时,EF 6中处理CRUD操作的正确方法是什么?

C# 在处理集合时,EF 6中处理CRUD操作的正确方法是什么?,c#,entity-framework,crud,C#,Entity Framework,Crud,我正在更新网页中的对象列表: 实体 public partial class MyParentType { public MyParentType() { this.children = new HashSet<child>(); } public int parentID { get; set; } public virtual ICollection<

我正在更新网页中的对象列表:

实体

    public partial class MyParentType
    {
        public MyParentType()
        {
            this.children = new HashSet<child>();
        }

        public int parentID { get; set; }

        public virtual ICollection<child> children { get; set; }
    }
积垢操作:

 public class MyRepository : IMyRepository
    {
        private readonly IErrorLogger _logger;
        private readonly CorporateEntities _context;

        public MyRepository(IErrorLogger logger)
        {
            _logger = logger;
            _context = new CorporateEntities();
        }

         public void Update(IEnumerable<MyParentType> parents)
 {
   try
   {
       foreach (var parent in parents)
       {
         if(parent.Id==0)
         {
          _context.MyParentTypes.Add(parent);
         }
         else
         { 
          _context.Entry(parent).State = EntityState.Modified;
          var removedChildren =
                            _context.Children.Where(
                                x => !fuelProcessing.Children.Select(
                                    y => y.ID).Contains(x.ID));
          _context.Children.RemoveRange(removedChildren);

            foreach(var child in parent.children)
            {
               context.Entry(child).State =child.Id>0? EntityState.Modified:EntityState.Added;  
            }
         }
       }

       _context.SaveChanges();
   }
   catch (Exception exception)
   {
     _logger.Error(exception.Message, exception);
   }
    }

}

添加新项目、更新现有项目和删除屏幕上已删除的项目的正确方法是什么?这似乎非常低效,我相信有更好的方法。

首先,如果使用虚拟,速度会非常慢,我建议您使用即时加载,而不是延迟加载

更新父级时,如果不更新子级,则不必加载子级


如果您还需要加载子项,则可以一次获取所有子项,而不是逐个获取,这样效率也更高。

我确实需要每次更新子项。您能给出一个模板化的例子吗?如果您需要更新子项,您可以立即加载它们。看看急切的加载。