2

I have a simple table with columns ID, REGDATE and EVENT_NAME. I've set SYSDATE as default value for the REGDATE column. If I insert a record right from SQL Developer current date is written to the REGDATE column. But if I insert a record with Entity Framework REGDATE column has NULL. Don't database defaults work when using Entity Framwork? What do I have to do?

Mikayil Abdullayev
  • 12,117
  • 26
  • 122
  • 206
  • 1
    In Sql your default value will only be inserted into the REGDATE column if you don't specify the column in your insert statement. If you look at the query that EF is generating when it inserts your value it's likely specifying that null should be inserted into the column, which is why you aren't getting the defaul. From what I've read, EF does not support SQL Default Values. IronMan's solution is the cleanest I've seen. – Ben Tidman Jan 11 '13 at 17:08
  • Additionally MS SQL only inserts a default value in a field if it is not null. Nullable fields do not use default values. To start, the database column would need to be not null. – Zachary Scott Apr 26 '20 at 23:20

2 Answers2

2

yo can set that properties,StoreGeneratedpattern to Computed and then EF won't update it, but then neither can you. This method will solve your Db default on the column.

Quinton Bernhardt
  • 4,773
  • 19
  • 28
  • The OP never said that he wants to maintain that value throug EF. So i made it clear that using this method has its downside. – Quinton Bernhardt Jan 11 '13 at 14:34
  • What you have suggested does really do what I need because the defaults I need to be written will never be changed again. But I still can't see the defaults written to the columns. – Mikayil Abdullayev Jan 11 '13 at 14:40
  • That _should_ work (http://stackoverflow.com/q/5042327/1099260). If you cannot get it to work then use IronMan's solution. If you don't want anyone to be able to set that value explicitly through EF i think you can, through the EF designer, change the property's `Setter` access modifier from `public` to `internal` or `private` - that way preventing updates (via the Entity) to that field. – Quinton Bernhardt Jan 11 '13 at 18:16
  • Because getdate() is non-deterministic (gives a different value for every call) it cannot be persisted (saved,) so I would expect this to always return the current datetime. – Zachary Scott Apr 27 '20 at 00:06
2

One way that you can do it is by having a constructor in your class to set that default automatically whenever you instantiate a new object of that type. This is similar to Ladislav's answer to this SO question.

Community
  • 1
  • 1
Corey Adler
  • 15,897
  • 18
  • 66
  • 80
  • To use a SQL server default value, the SQL field must be `not null`, otherwise SQL inserts a null value. To have Entity Framework insert the SQL server default, EF must submit a null value for that field. This begs to ask must the EF entity define the field as nullable even though the database field is not null for EF to submit a null value for the SQL server default to apply? – Zachary Scott Apr 26 '20 at 23:27