Getdate () does not work with Entity Framework

So, I have a column called Timestamp in my database with the default GetDate() .

When I use the Entity Framework to insert a record, the Timestamp is null .

Why is this so?

+4
source share
3 answers

Try changing the type to datetime2. See Article for more information. Also try using Calculated storegeneratedpattern

try switching to datetime2 and then the workaround by adding a trigger

  SET NOCOUNT ON; UPDATE ASBLAH SET YourFieldChangeTime = getdate() WHERE YOURId IN(SELECT AS_ID FROM INSERTED) 

If you need to read data back from this date, you can do it after the context .SaveChanges ()

 context.Refresh(System.Data.Objects.RefreshMode.StoreWins, p); 
+2
source

edit your EDM, make the following changes:

 Type="datetime" Nullable="false" StoreGeneratedPattern ="Computed" ref: http://bibby.be/2009/07/entity-framework-sql.html 
+3
source

The Entity Framework probably creates an insert statement that includes all fields, with null being the value for Timestamp since you did not specify it.

If null allowed for this column, then this is what will be inserted. If null not allowed, the default value that will be allowed for GetDate() will be used.

Do I need to allow null for your table?

+1
source

Source: https://habr.com/ru/post/1398696/


All Articles