Using EF 6.1, I want to set the foreign key to null (break the connection between two objects), without asking for any object in the first place. Preferably, I also do not want to specify the identifier of the associated object (since this requires adding it to my HTML).
My current code does not make unnecessary queries, but requires an identifier for the associated object:
public void RemoveCurrentTeacherOfGroup(int groupId, int teacherId)
{
var group = new Group { Id = groupId };
var teacher = new Teacher { Id = teacherId };
group.Teacher = teacher;
_dataContext.Groups.Attach(group);
_dataContext.Teachers.Attach(teacher);
group.Teacher = null;
_dataContext.SaveChanges();
}
However, I do not need teacherId, groupId enough information.
Additional Information: The database was generated using code. Entities are defined as follows:
public class Teacher
{
public int Id { get; set; }
..
public virtual List<Group> Groups { get; set; }
}
public class Group
{
public int Id { get; set; }
..
public virtual Teacher Teacher { get; set; }
}
Is there a way to set the foreign key to null without the "teacherId?"
, , , , - , null.