How do I tell Entity Framework to allow SQL Server to provide a specific default value for a field?

I used the following SQL script to enable setting the current time on a field when a new row is added to the table:

ALTER TABLE [Items] ADD CONSTRAINT DF_Items DEFAULT GETDATE() FOR [CreationDate]

      

Now I am using Entity Framework to work with this table and add new rows to it. I want this column to get its value from SQL Server itself and not have to provide the value itself. Setting this column value to Nothing

in Visual Basic fills the field with using DateTime.MinValue

, which is not what I want (and by the way, SQL Server does not support).

What changes should I make to make this work?

0


source to share


1 answer


You must set StoreGeneratedPattern

in the EDMX designer (or DatabaseGeneratedOption

at the beginning of the code) before Identity

for this date property. EF always sends a .NET default for a blank property that is not generated by the store. Setting the template to on Identity

will tell EF that the value is generated in the DB at the time of the insert, and it will ask for its value. If you change the default template, you will not be able to install it in your application.



+5


source







All Articles