简体   繁体   English

使用实体框架更新表中记录列表的正确方法

[英]Proper way to update the list of records in table using entity framework

In my Education Programme database table, I have 3 records for one specific student在我的教育计划数据库表中,我有一个特定学生的 3 条记录

在此处输入图片说明

so I want to remove P3 program from this person list, so in font-end its showing like like this所以我想从这个人列表中删除P3程序,所以在字体结束时它的显示是这样的

在此处输入图片说明

once I click Delete, specific P3 remove from frontend and model and once hit save button it's passing to the backend code P1 , P2 as a list.一旦我点击删除,特定的P3从前端和模型中删除,一旦点击保存按钮,它就会作为列表传递给后端代码P1P2

I'm using the entity framework for database transactions.我正在使用实体框架进行数据库事务。

looking for your advice here on在这里寻找您的建议

  • Should I delete all existing records and then insert P1 , P2 again.我是否应该删除所有现有记录,然后再次插入P1P2 Or或者
  • DO I need iterate each record by comparing list I'm sending vs existing records and attach P1 , P2 and find out missing one and delete Or我是否需要通过比较我发送的列表与现有记录来迭代每条记录并附加P1P2并找出丢失的一个并删除或
  • this type of transaction can EF do itself这种类型的交易可以 EF 自己做

Looking forward which way is recommended期待推荐哪种方式

I would suggest you to go for second option since its better approach to remove missing records rather than remove all records.我建议您选择第二个选项,因为它是删除丢失记录而不是删除所有记录的更好方法。

'Missing' items from a collection are not considered to be deleted.集合中的“丢失”项目不被视为被删除。

So what you'll need to do is mark the items for deletion yourself.因此,您需要做的是自己标记要删除的项目。 Something like this:像这样的东西:

public void Update(Programme record)
{
    var missingRows = dB.ProgrammeRows.Where(i => i.ProgrammeId == record.ProgrammeId)
                        .Except(record.Rows);
    dB.ProgrammeRows.RemoveRange(missingRows);

    dB.Programmes.Update(record);
    dB.SaveChanges();
}

And the class should be as follows:该类应如下所示:

   public class ProgrammeRow
   {
        public int ProgrammeId { get; set; }
        public string ProgrammeName { get; set; }
        public int StudentId { get; set; }
        public virtual Student Student{ get; set; }
   }

   public class Programme
   {
        public int ProgrammeId { get; set; }
        private ICollection<ProgrammeRow> _rows;
        public virtual ICollection<ProgrammeRow> Rows => _rows ?? (_rows = new List<ProgrammeRow>());
   }

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

相关问题 如何使用 Entity Framework Core 的 Linq 更新表中的多个记录 - How to update multiple records in a table Using the Linq for Entity Framework Core 如何更新实体框架中表的所有记录? - How to update all the records of a table in an entity framework? 在Entity Framework 4.3中更新实体属性的正确方法-代码优先 - Proper Way to Update Properties of an Entity in Entity Framework 4.3 - Code First 删除实体框架上下文中的所有记录并重新创建它的正确方法是什么? - What is the proper way to delete all records in an Entity Framework context and recreate it? 实体框架:更新相关表的最佳方法 - Entity Framework: Best way to update related table 我想要一种方法来根据实体框架中的 where 子句更新一系列记录,而不使用 ToList() 和 foreach - I want a way to update a range of records based on a where clause in entity framework without using ToList() and foreach 使用实体框架更新记录的最佳方法 - Best way to update a record using Entity Framework 使用.NET 4实体框架删除引用的正确方法是什么? - What is the proper way to remove a reference using the .NET 4 Entity Framework? 使用实体框架更新表中的单行 - Update single row in a table using entity framework 使用 Entity Framework Core 更新列表 - Update list using Entity Framework Core
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM