2

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.

1 Answer 1

3

You must use DatabaseGenerationOption.Identity.

Here are two links that explain further:

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks paulsm4, DatabaseGeneratedOption, as per Ladislav's post solved but: 1. I couldn't chose 'Identity' (more relevant to me as this data is not to be later updated) as I was getting 'Validation failed for one or more entities' error, hence I had to switch to 'Computed' to remove the error but I don't quite understand why. 2. What is it '[Column(IsDbGenerated = true)]' supposed to be used for then?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.