Relations are not zero

I have a very simple EF operation that fails: break the connection between two objects, as shown in the code below:

public async Task RemoveCurrentTeacherOfGroup(int groupId)
{
    var group = await _dataContext.Groups.SingleAsync(g => g.Id == groupId);
    group.Teacher = null;
    await _dataContext.SaveChangesAsync();
}

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; }
}

However, breaking up does not work; Teacher continues to point to the same person. When navigating with the debugger, I see that the Teacher property does not become null after .Teacher = null. I tried this with a synchronous alternative that had the same effect:

public void RemoveCurrentTeacherOfGroup(int groupId)
{
    var group = _dataContext.Groups.Single(g => g.Id == groupId);
    group.Teacher = null;
    _dataContext.SaveChanges();
}
+4
source share
1 answer

If Teachernot uploaded, you cannot break the connection. Or include it (eager-load) in the request:

_dataContext.Groups.Include(g => g.Teacher).Single(g => g.Id == groupId);

, , , null:

var teacher = group.Teacher;
group.Teacher = null;

, "Teacher null null", (lazy-load it) , null.

null , group.Teacher = null, ( , , EF , ). , , , Teacher

+5

All Articles