How to use the standard Entity Framework and default date values

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

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

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

The problem is that when using the Entity Framework and matching the Date field, the Entity Framework inserts the default DateTime.Min when instantiating the object.

I cannot match the Date field with a null DateTime value, that is, DateTime ?, and I cannot use CONVERT or DateTime.Now.Today in the standard Entity Designer value, since it only accepts hard-coded constant values.

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

Are there other ways to achieve what I want when the default value that is stored in the database table is used instead?

+12
c # entity-framework
Oct 22 '09 at 18:59
source share
8 answers

I just ran into this - I worked around it, setting the date (the field that I needed was automatically generated) in the Entity constructor using a partial method. Regardless of whether it is Ideal or whether it works in all cases, it is not yet visible, but it has fixed my problem so far.

+1
Oct 28 '09 at 14:23
source share

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

  public partial class YourDBObject { public YourDBObject() { this._DateField = DateTime.Now; } } 
+14
Jan 11 '10 at 15:41
source share

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 that you want. This article

+5
Oct 22 '09 at 21:10
source share

This is really unsuccessful. The answers that reference StoreGeneratedPattern are not a true solution that does not allow you to set a value.

The whole team will have to do something like this:

  [Required] [DefaultValue] // suggested attribute, whatever you want to call it public DateTime Created { get; set; } 

If the field is marked with DefaultValue, then during EF SQL generation time, properties with this attribute will be checked, if field == default(DateTime) , if so, then simply omit this column from the generated code.

+2
Jan 07 '14 at 0:28
source share

I had a similar problem using RIA services with Entity Framework. I was not able to set the value in the default constructor on the client side, because the object classes created there already have an empty default constructor.

The solution I used was to implement the OnCreated partial method for the object on the client, setting the required default value.

+1
May 20 '11 at a.m.
source share

Can a date field display in DateTime? only if the column is NULL in the database.

0
Oct 24 '09 at 1:58
source share

I think the answer provided by lazyberezovsky to another SO question is a direct solution to this Entity Framework problem and the default date

0
Aug 09 '13 at 7:52
source share

Just what happened with that. Entity Framework does not support default values ​​on its own. You need to set the property StoreGeneratedPattern = Calculated in the column in the element of the Edmx file. The default value that you set in the database will be preceded by what EF sets by default, i.e. DateTime.Min

0
Dec 01 '17 at 9:50
source share



All Articles