How to utilize Entity Framework's default date values and Entity Framework's default entity framework

c# entity-framework

Question

I have a data table with a date column that has the default value of in my SQL Server database design.

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

This is perfect for adding the date automatically to a new record while adding entries manually to the database.

My issue is that the Entity Framework adds a default value of DateTime.Min when the entity is constructed when mapping the Date field using the Entity Framework.

I am unable to utilize CONVERT or DateTime, nor am I able to map the Date field to a nullable DateTime, i.e. DateTime? Now. Currently, the Entity Designer only takes values that have been hard-coded as constants.

Whenever the object is formed, I can of course manually set the DateTime field in C#, either directly in code, in the constructor of a partial class, or even during the saving changes event.

Exist any other approaches that truly leverage the database table's default value computation to accomplish what I want?

1
13
10/22/2009 6:59:48 PM

Accepted Answer

I recently encountered this issue, and I was able to get around it by utilizing a partial function to change the date (the field I required to be automatically created) in the Entity's constructor. It remains to be seen if this is Ideal or whether it is effective in all situations, but so far, my issue has been resolved.

1
10/28/2009 2:23:17 PM

Popular Answer

For your EntityObject, create a partial class, include a default constructor, and specify default values in it.

    public partial class YourDBObject
    {
          public YourDBObject()
          {
               this._DateField = DateTime.Now;
          }
    }


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow