Reputation:
I am using Entity Framework 5 with Code First, I know how to create new value no problem and for the most part update existing data with new info but what I can't figure out is how to update an existing value to NULL. The situation is that I am removing the existing value that is in the DB, the column does allow NULL values. How do I tell EF to replace the current value of the column with a NULL value?
Upvotes: 0
Views: 125
Reputation: 149030
Simply set the value of the property you want to make null, and save the changes:
var myRecord = dbContext.MyTable.First(...);
myRecord.MyColumn = null;
dbContext.SaveChanges();
Of course, the data type of the property must be nullable -- e.g. Nullable<int>
rather than int
.
If you want to delete the entire record, use the Remove
method provided by the collection:
var myRecord = dbContext.MyTable.First(...);
dbContext.MyTable.Remove(myRecord);
dbContext.SaveChanges();
Upvotes: 1