Właściwość nawigacji ustawiania Entity Framework na wartość null
Mam pierwszy projekt bazy danych encji. oto wyciąg modelu:
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; }
}
Mam problem z ustawieniem elementu nawigacyjnegoResponsibleUser
klasyLedProject
. Kiedy ustawięResponsibleUser
do innego użytkownika, a następnie zapisz zmiany w DBContext, zmiany są przechowywane w bazie danych.
Ale kiedy chcę usunąć bieżącyResponsibleUser
zLedProject
, ustawiając właściwość nawigacji na wartość null. Zmiany nie są przechowywane w bazie danych.
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();
Czy istnieje jakaś sztuczka do usuwania właściwości nawigacji?