[英]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从前端和模型中删除,一旦点击保存按钮,它就会作为列表传递给后端代码P1 , P2 。
I'm using the entity framework for database transactions.我正在使用实体框架进行数据库事务。
looking for your advice here on在这里寻找您的建议
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.