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

entity-framework-6 sql-server

Question

Table T2 in SQL Server contains two columns:

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

Because it utilises (getdate()) by default, this line successfully adds the CreateDate value.

Insert T2 (Id)
Values (1)

All is OK thus far. Using Entity Framework to enter a row but still wanting to utilise the default (getdate() ) value.

Due to theCreateDate as definedNOT NULL , When using EF, I am unable to leave it empty or leave it out of the Insert statement. But I want the server/database side of SQL to provide the timestamp.

Is there a solution for this?

1
2
7/25/2018 9:05:54 PM

Accepted Answer

I was able to discover the solution because to squillman's a mention of another SO post.

You can do what I'm attempting to do by setting the StoreGeneratedPattern attribute in the EDMX diagram.

There are three options generated by a database.

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.

EDIT: I had to modify Identity to Computed even though the image displays Identity. The Identity option only works if the row is Inserted, which is the cause. Any time the row is modified (other columns are updated), an error occurs. The default script is executed when using the Computed option with Insert and Updates (to other columns, default script does not run again).

enter image description here

0
7/28/2018 10:35:02 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow