简体   繁体   English

从具有多对多关系的实体中删除对象

[英]Delete object from an entity with many to many relationship

I am new to Entity Framework so I need help with deleting an object from an entity.我是实体框架的新手,所以我需要帮助从实体中删除对象。 I have 2 tables which are in many to many relationship and an association table connecting them in the database.我有 2 个多对多关系的表和一个在数据库中连接它们的关联表。 In the model there are only two tables and the association one is presented by navigation properties as this is how the EF works.在模型中只有两个表,关联之一由导航属性呈现,因为这就是 EF 的工作方式。 Now I need to delete an object from the first table by context.EntityName.DeleteObject(object) but when I try to do it the code fails with error "The DELETE statement conflicted with the REFERENCE constraint FK..", which is a foreign key from the association table to the entity, which object I try to delete.现在我需要通过context.EntityName.DeleteObject(object)从第一个表中删除一个对象,但是当我尝试这样做时,代码失败并显示错误“DELETE 语句与 REFERENCE 约束 FK 冲突 ..”,这是一个外来的从关联表到实体的键,我尝试删除哪个对象。 I wonder how to fix this.我想知道如何解决这个问题。 Could you please help me?请你帮助我好吗?

Here is how the tables look like:下面是表格的样子:

Teachers老师

  • Teacher_ID教师_ID
  • FirstName
  • LastName

TimetableDetail时间表详情

  • TimetableDetail_ID时间表详情_ID
  • EducationalDiscipline_ID EducationalDiscipline_ID
  • Weekday工作日
  • StartTime开始时间
  • Duration期间

and the associaion table:和关联表:

TimetableDetailTeachers时间表详细教师

  • Teacher_ID教师_ID
  • TimetableDetail_ID时间表详情_ID

And here is how I try to delete it:这是我尝试删除它的方法:

TimetablesEntities context = new TimetablesEntities();

TimetableDetail detail = context.TimetableDetails.SingleOrDefault(td => td.TimetableDetail_ID == timetableDetailId);

context.TimetableDetails.DeleteObject(detail);

context.SaveChanges();

Thanks in advance!提前致谢!

You just need to clear the association table by clearing the Teachers list for a particular TimetableDetail .您只需要通过清除特定TimetableDetailTeachers列表来清除关联表。 Using your code...使用您的代码...

TimetablesEntities context = new TimetablesEntities();

TimetableDetail detail = context.TimetableDetails.SingleOrDefault(td => td.TimetableDetail_ID == timetableDetailId);

detail.Teachers.Clear();

context.TimetableDetails.DeleteObject(detail);

context.SaveChanges();

The key line being detail.Teachers.Clear()关键是detail.Teachers.Clear()

Yeah, this is a tricky one.是的,这是一个棘手的问题。

What you need to do is clear the entities out of the EF's underlying local storage.您需要做的是从 EF 的底层本地存储中清除实体。 The example shows what to do when you clear all details for a specific teacher (or even only some details) and then save that teacher's entity.该示例显示了当您清除特定教师的所有详细信息(或什至只是部分详细信息)然后保存该教师的实体时要执行的操作。 With that in mind, here is some example repository code:考虑到这一点,以下是一些示例存储库代码:

public void EditTeacher(Teacher teacher)
{
    if (teacher == null)
    {
        throw new ArgumentNullException("teacher");
    }

    YourDbContext.Entry(teacher).State = EntityState.Modified;

    // Remove all timetable details that have an orphaned relationship.
    // (E.g., orphaning occurs when 'teacher.TimetableDetails.Clear()'
    //  is called or when you delete one particular TimetableDetail
    //  entity for a teacher)
    YourDbContext.TimetableDetails
        .Local
        .Where(td => td.Teacher == null)
        .ToList()
        .ForEach(td => YourDbContext.TimetableDetails.Remove(td));

    YourDbContext.SaveChanges();
}

I hope this helps.我希望这有帮助。

For further reading, take a look at: Deleting orphans with Entity Framework.如需进一步阅读,请查看:使用实体框架删除孤儿。

Edit:编辑:

The example code above assumes that elsewhere in your code you have defined and created YourDbContext as a member variable within your repository class.上面的示例代码假设您在代码的其他地方定义并创建了YourDbContext作为存储库类中的成员变量。 I just wanted to point that out in case it wasn't clear.我只是想指出这一点,以防它不清楚。

You could do: add row before remove detail:您可以这样做:在删除详细信息之前添加行:

context.Teachers.RemoveRange(detail.Teachers);

The key line being that...关键是...

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM