EF - pass null value to SQL column so default value gets inserted

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


EF - pass null value to SQL column so default value gets inserted



SQL Server table T2 has 2 columns:


Id INT NOT NULL
CreateDate DateTime NOT NULL, default = (getdate())



This statement inserts the CreateDate value correctly because it uses (getdate()) as default.


Insert T2 (Id)
Values (1)



So far so good. The problem is when I use Entity Framework to insert a row and still wish to use the default (getdate()) value.


getdate()



Because the CreateDate is defined as NOT NULL, I cannot leave it blank or leave out of the Insert statement when using EF. But I want SQL to generate the timestamp on the server/database side.


CreateDate


NOT NULL



Is there a way to handle this?





Slight detour...the name timestamp is a bad choice for a column name. It is the name of an existing datatype (which has nothing to do with time of day) and also it is incredibly ambiguous. I sense it is a time that something happened but what? The date it was inserted? The date it was updated? Use a meaningful name like DateCreated or something.
– Sean Lange
yesterday





Sorry, it is not really called Timestamp. I was trying to write this up quick and strip down to basics. Agreed, bad name choice. I changed to CreateDate. Thanks.
– nanonerd
yesterday





Ahh gotcha....no biggie. I can't help with the EF stuff....I can't even spell it. :)
– Sean Lange
yesterday





This question might help: stackoverflow.com/questions/5341238/…
– squillman
yesterday





Bravo squillman. I found the answer in that link. I posted full answer below. Thanks !
– nanonerd
19 hours ago




1 Answer
1



Thanks to squillman's reference to another SO post, I was able to find the answer.



Go to EDMX diagram, and you can set the StoreGeneratedPattern property to achieve what I am trying to do.



There are three Database Generated Options


Computed : The database generates a value when a row is inserted or updated.
Identity : The database generates a value when a row is inserted.
None : The database does not generate values.



enter image description here






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Keycloak server returning user_not_found error when user is already imported with LDAP

PHP parse/syntax errors; and how to solve them?

How to scale/resize CVPixelBufferRef in objective C, iOS