Reputation: 409
I have read several post around the problem but found not solution to the issue I'm facing with.
My entity model contains several date properties whose values I need to be set at SQL server level. Here's an example:
[Column(IsDbGenerated = true)]
public DateTime DateCreated { get; set; }
DateCreated is a 'date' type on SQL Server, and its default value is GETDATE().
[DateCreated] DATE DEFAULT (getdate()) NOT NULL,
As a matter of fact saving a new record (without passing any DateCreated value) results in '1/1/0001' (i.e. null datetime) value being inserted. It looks like Linq overrides default server GETDATE() value, forcing a 'null' value to be written.
Upvotes: 2
Views: 308
Reputation: 121749
You must use DatabaseGenerationOption.Identity
.
Here are two links that explain further:
Upvotes: 3