views:

284

answers:

4

In my SQL Server database schema I have a data table with a date field that contains a default value of

CONVERT(VARCHAR(10), GETDATE(), 111)

which is ideal for automatically inserting the date into the new record when manually entering records in to the database.

The problem I have is that when using the Entity Framework and mapping the Date field, that the Entity Framework inserts a default value of DateTime.Min when the entity is instantiated.

I cannot map the Date field to a nullable DateTime, i.e. DateTime?, nor am I able to use either CONVERT or DateTime.Now.Today in the default value of the Entity Designer as it only accepts hard-coded constant values.

I can of course explicitly set the DateTime field in C# whenever the object is created, either explicitly in code, in the constructor of a partial class, or even during the saving changes event.

Are there any other ways of achieving what I want where the default value calculation stored in the database table is actually used instead?

A: 

You can use a database trigger, which, on insertion, checks if the inserted value is DateTime.MinValue (01.01.0001) and replaces it with the calculated value you desire. This MSDN article describes how to create a trigger.

A little "flaw": with this solution you would have to store the new object to the database and read it back afterwards to update your object with the calculated value.

andyp
Good point. That would work too. But it just feels like I am wielding a sledgehammer to crack a walnut.
Justin
A: 

The date field can be mapped to DateTime? only if the column is nullable in the database.

kubal5003
But if the column is nullable, then logic dictates you cannot have a "default value." And this is the behaviour as exhibited by the SQL server.
Justin
+1  A: 

I just ran into this - I worked around it by setting the date (the field I needed auto generated) in the constructor of the Entity using a partial method. Whether this is Ideal or whether it works in all cases is yet to be seen, but it has fixed my problem so far.

Adam Tolley
This is the solution I finally chose. It seems a bit of a kludge, but it works flawlessly. A shame that such an oversight is present in the EF.
Justin
Glad to have helped, Lets hope EF4 fixes this bit of pain. If you think this is the best solution your likely to find please mark it as the answer (or at least upvote it) so I can get some credit - I'm trying to build at least enough rep to use this site a little more actively. Thanks!
Adam Tolley
I would do that, but it requires 15 reputation. Sorry.
Justin
If you do this won't the date being stored be the one where the EF context code is running (potentially the client), rather the database server. Meaning a user could 'fake' dates by changing the time on their PC.
AndyM
+3  A: 

Create a partial class for your EntityObject, add a default constructor, and set default values in it.

    public partial class YourDBObject
    {
          public YourDBObject()
          {
               this._DateField = DateTime.Now;
          }
    }
Chase
Exactly what I needed (though not for this question)! :D
FerretallicA
Thanks. This works, although it confused me at first because the constructor also gets called when existing objects are fetched from the database - however in those cases the `DateTime.Now` gets overwritten with the database values after construction.
codeulike