Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework: Updating a field to a null value?

I'm using stubs to update my entities and when the updated entity consists of columns that have values changed from non-nulls to nulls, the nulls are not persisted to the database i.e. the record continues to hold the previous non-null values.

What am I doing wrong?

public void UpdateEntity(Entity e)
        {
            _context.Works.Attach(new Entity{ Id = e.Id });
            _context.ApplyCurrentValues("Entities", e);
            _context.SaveChanges();
        }
like image 270
Amit G Avatar asked Nov 05 '22 03:11

Amit G


1 Answers

The problem is that you need to assign null to these properties after you Attach(), not before. Perhaps ApplyCurrentValues() only copies non-already-identical properties? (I've never tested, but it would be reasonable if it did.)

like image 88
Craig Stuntz Avatar answered Nov 14 '22 21:11

Craig Stuntz