4

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;
    }
}
5
  • 1
    without savechange any change won't affect the db Commented May 5, 2016 at 18:51
  • 1
    Did you just make the FK nullable in your class without changing the database? Commented May 5, 2016 at 18:53
  • I'll update the post to suply more information. Commented May 5, 2016 at 19:11
  • Which EF version is this? Commented May 5, 2016 at 21:16
  • EF6, having exactly the same problem, cannot understand why. Interesting, if before saving the changes I assign null to the property in the Immediate Window, the property correctly sets to null. Commented Jun 30, 2016 at 5:03

1 Answer 1

1

I would say you most likely did not update the database itself. Once you update the database using this code:

ALTER TABLE [table_name] ALTER COLUMN [fk_column] [datatype] NULL

Run 'Update Model from Database' on the .edmx file. enter image description here

Make sure this Nullable = true enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

I'm using Migration to maintain the DB, and the FK already is nullable... the problem it's on EF that not persist the null value on the db after the attribute has any non-null value.
Is this on a save or edit action? You should check the entity state to make sure the object is modified before inserting/updating it.
I've to pass these fields that i want to force the null value, to set it as IsModified = true; after i Attach. ` DbSet.Attach(entityToUpdate); foreach (var item in updateNullFields) { Context.Entry(entityToUpdate).Property(item).IsModified = true; } `

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.