I have 2 POCO classes (below) and you want to remove the link between my two posts. According to this, EF 5.0 should be able to handle deletion without loading the User class as follows:
context.Computers.Find("test").User = null; context.SaveChanges();
This does not work, but using the approved .net 4 method, it works:
en = context.Computers.Find("test"); context.Entry(en).Reference(e => e.User).Load(); en.User = null; context.SaveChanges();
The reference to EF is EntityFramework.dll version 5.0.0.0. Did I miss something obvious here?
Here are the classes:
public class Computer { public string Id { get; set; } public Nullable<int> UserId { get; set; } public virtual User User { get; set; } } public class User { public int Id { get; set; } public virtual ICollection<Computer> Computers { get; set; } }
Edit: Here are the specific lines in the above related article that do not seem to be consistent with the functionality that I see:
To unlink, set the navigation property to null. If you are working with the Entity Framework based on .NET 4.0, you must bind the end associated with it before you set it to null. For instance:
context.Entry(course).Reference(c => c.Department).Load(); course.Department = null;
Starting with Entity Framework 5.0, based on .NET 4.5, you can set the null relationship without loading the bound end.
source share