Skip to content Skip to sidebar Skip to footer

Entity Framework Not Claiming Datetime.now Is Null

Here is my code: UVCUpdate update = new UVCUpdate(); update.CurrentDate = DateTime.Now; _context.UVCUpdates.Add(update); _context.SaveChanges(); Now I am getting an inner excepti

Solution 1:

It almost always happens when there is mismatch between so called "store generated pattern" between EF model and database. If model column has store generated pattern of Identity or Computed - that means EF will be sure those values will be automatically provided by database on insert or update, and there is no need to include them in INSERT or UPDATE statements. Missing values will have default NULL value, and if this column is non-nullable in database at the same time, and is not really computed or identity - you have the error in question.

Solution 2:

I am sorry guys, I feel like an idiot. Thank you to Leopard.

I went to get the UVCUpdate class to show him and realized that when I copied that class from another class I accidentally left [DatabaseGenerated(DatabaseGeneratedOption.Identity)] in the class for the CurrentDate. So it was likely attempting to send a null value to the SQL so the SQL would create an ID, but the field was set to not allow nulls. CurrentDate was not supposed to be an ID so I removed that and now it works...

Post a Comment for "Entity Framework Not Claiming Datetime.now Is Null"