Scenario:
I have an entity with a (nullable) FK Id that it's not required anymore.
Problem:
When I update this entity on the context, setting NULL to the FK, after the SaveChanges() the value is not persisted on the db.
Any ideas?
Edit #1:
After the SaveChanges, I checked the base and continues the old value, and if I change to other value works fine.
Class:
public class Account
{
[Required]
public int Id { get; set; }
public int? PlanId { get; set; }
[Required]
public virtual Status Status { get; set; }
#region Navigations Properties
public virtual Plan Plan { get; set; }
#endregion
}
Usage: (Problem)
Account ent = Context.Set<Account>().AsQueryable().FirstOrDefault(x => x.Id = id);
ent.PlanId = null;
Context.SaveChanges();
Edit [PROBLEM SOLVED]
It was necessary to send an array with the fields that would get null as the new value.
public virtual void Update(TEntity entityToUpdate, List<string> updateNullFields)
{
DbSet.Attach(entityToUpdate);
foreach (var item in updateNullFields)
{
Context.Entry(entityToUpdate).Property(item).IsModified = true;
}
}

