Default value is datetime2, entity structure

I have a table with datetime2 column type (it was datetime, but EF, as I see it, works with datetime2). I set it to null and with a default value SYSDATETIME(). For normal operation, I set a property that maps to this column in the EF model as Computed. Now, when I do not set any value in this property, I can see the notmal entry in the table, but when I try to set the value from the code, it ignores it and in all cases sets it SYSDATETIME().

How can I insert the default value that is set in the database when there is no value in the property in the model and the value of the property if it is not null and set?

EDIT: Here is a sample code

.....
ActionsJournal actionsJournalEntry =
 TestFactory.CreateActionEntry(.....);

if (/* some condition */)
{
  actionsJournalEntry.CreatedDate = DateTime.Now.AddDay(30); // else it will be null
}

ent.ActionsJournals.AddObject(actionsJournalEntry);

ent.SaveChanges();
....
+5
1

: .

StoreGeneratedPattern.Computed , StoreGeneratedPattern.None . , Computed, , None , = - 1.1.0001 , EF DATETIME2.

- /EF, DateTime.

:

StoreGeneratedPattern None:

public partial class ActionsJournal 
{
    public class ActionsJournal() {
        CreatedDate = DateTime.Now.AddDay(30);
    }
}

CreatedDate, .

+4

All Articles