Linq to Sql: default SQL default
I have read several posts about this problem but did not find a solution to the problem I was facing.
My entity model contains several date properties that need to be set at the SQL server level. Here's an example:
[Column(IsDbGenerated = true)]
public DateTime DateCreated { get; set; }
DateCreated is a date type in SQL Server and its default is GETDATE ().
[DateCreated] DATE DEFAULT (getdate()) NOT NULL,
In fact, saving a new record (without passing any DateCreated value) results in the insertion of the value "1/1/0001" (ie null datetime). It looks like Linq is overriding the server's default GETDATE () by forcing it to be "null".
You must use DatabaseGenerationOption.Identity
.
Here are two links that explain further:
-
Entity Framework Structure Annotations First Data Annotations
-
How do I tell Entity Framework to allow SQL Server to provide a specific default value for a field?