5

I have a table with column type of datetime2 (it was datetime, but EF as I see works with datetime2). I have set it as not null and with default value of SYSDATETIME(). In order to work normaly I have set the property which maps to this column in EF model as Computed. Now when I am not seting any value in that property I can see notmal record in table, but when I try to set the value from code it ignores it and in all cases set SYSDATETIME().

How it is possible to insert default value which is set in DB when no value is in the property in model and value of the property if it is not null and set?

EDIT : Here is 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();
....
6
  • Give an example where you insert datetime2 value. Commented Aug 17, 2011 at 4:50
  • @Kak Ylia ... you need code sample or business case ? And how it is connected to the question ? Commented Aug 17, 2011 at 5:44
  • @NDeveloper, code samples are always relevant to questions on SO. I want to see some code demonstrating the issue as well. Commented Aug 17, 2011 at 5:50
  • @Inuyasha question now includes sample code ... the issue is that it does not care whether there is any value in actionsJournalEntry.CreatedDate it just inserts the DB default value, which is SYSDATETIME(). Commented Aug 17, 2011 at 6:18
  • How are you doing your mapping? In SSDL, there are two different attributes, StoreGeneratedPattern and DefaultValue. If StoreGeneratedPattern is Computed, then you're saying that the database always sets/resets this value. Commented Aug 17, 2011 at 6:30

1 Answer 1

4

Short answer: It is not possible without workaround.

You must either set StoreGeneratedPattern.Computed and always set value in the database or you must set StoreGeneratedPattern.None and always set value in the application. The difference is that if you set Computed it never passes your value in update or insert statements whereas None passes it always even if you don't set it = results in default date which is 1.1.0001 and the reason why you think that EF uses DATETIME2.

The other problem is design flaw in designer / EF which doesn't allow you setting default value for DateTime.

Workaround:

Set default value for your date time in custom parameter less constructor of your entity and set StoreGeneratedPattern to None:

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

Now you will always have default value for your CreatedDate unless your application or loading from database overwrite it with another value.

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

3 Comments

As I understand value can be set from DB or Application only. There is no possibility to have default value in DB and optionlly overwrite it with value passed from application
Man...this sucks, I mean, EF 4.1+ is awesome, but this something pretty common to not take it under consideration.
So Microsoft said; "Lets make a new type called DateTime2 that is worse that the type it is intended to replace". Why why why why why!

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.