Entity Framework set navigation property to null

c# entity-framework

Question

My first project is an entity framework database. the model's extraction is shown here:

public partial class LedProject
{
    public LedProject()
    {
        this.References = new HashSet<LedProjectReference>();
        this.Results = new HashSet<LedProjectResult>();
        this.History = new HashSet<LedProjectHistory>();
    }

    public string Identifier { get; set; }
    public string Name { get; set; }
    public Nullable<System.DateTime> CompletionDate { get; set; }
    public System.DateTime CreationDate { get; set; }
    public System.Guid ProjectId { get; set; }
    public string Comment { get; set; }

    public virtual User ContactUser { get; set; }
    public virtual User CreationUser { get; set; }
    public virtual Customer Customer { get; set; }
    public virtual LedProjectAccounting Accounting { get; set; }
    public virtual LedProjectState State { get; set; }
    public virtual ICollection<LedProjectReference> References { get; set; }
    public virtual ICollection<LedProjectResult> Results { get; set; }
    public virtual User ResponsibleUser { get; set; }
    public virtual ICollection<LedProjectHistory> History { get; set; }
}
public partial class User
{
    public System.Guid UserId { get; set; }
    public string LoginName { get; set; }
    public System.DateTime CreationDate { get; set; }
    public string Firstname { get; set; }
    public string Lastname { get; set; }
    public string Email { get; set; }
}

Setting the navigational item is a challenge for me.ResponsibleUser of the groupLedProject In setting theResponsibleUser the DBContext to a different user and then save the DBContext modifications, the changes are recorded in the database.

However, if I wish to remove the currentResponsibleUser of anLedProject by the navigation property being set to null. The modifications are not kept in the database.

LedProject project = db.LedProject.Find(projectId);
project.Name = string.IsNullOrEmpty(name) ? null : name;
...
project.ResponsibleUser = responsibleUser == null ? null : db.User.Find(responsibleUser.UserId);
...
db.SaveChanges();

Is there a secret to getting rid of the navigation properties?

1
23
8/6/2016 3:11:55 PM

Accepted Answer

The navigation property's sluggish loading is the source of the issue. According to appearances, the value is first set to null and then loaded from the database. Therefore, the value already recorded in the database overrides the intended value (in my example, null).

LedProject project = db.LedProject
    .Include("ResponsibleUser")
    .Where(p => p.ProjectId == projectId)
    .FirstOrDefault();

When the Project is loaded, this loads the ResponsibleUser. Finally, this resolved my problem!

41
10/11/2012 7:42:23 AM

Popular Answer

The slow loading is the issue, as boindiil already mentioned. However, in order for the Entity Framework machinery to recognize that a change has been made, you must load the property before you can null it. The code may resemble:

responsibleUser = responsibleUser == null ? null : db.User.Find(responsibleUser.UserId);
if (responsibleUser == null)
{
    // load the value to assure setting it to null will cause a change
    var dummy = project.ResponsibleUser; 
}

project.ResponsibleUser = responsibleUser;
...
db.SaveChanges();

I kept thinking that with db.ChangeTracker there should be a method to force a save without loading, but I haven't found it yet (and the few things I have tried seemed really hacky).



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow