How to manage GetDate() with Entity Framework

前端 未结 5 1244
一生所求
一生所求 2021-02-07 03:52

I have a column like this in 1 of my database tables

DateCreated, datetime, default(GetDate()), not null

I am trying to use the Entity Framewor

5条回答
  •  渐次进展
    2021-02-07 04:25

    I had the same problem! For me it works like this:

    Database MS SQL Server express:

    [RowTime]  [datetime2](7)  NOT NULL
    
    ALTER TABLE [dbo].[Table_1] ADD  CONSTRAINT [DF_Table_1_RowTime]  DEFAULT (getdate()) FOR [RowTime]
    GO
    

    Then I import the table from database to my Entities model. Entities will not realise the default value! So, you have to set the StoreGeneratedPattern of the column to Computed. Then Entities will not put there any default value any more.

    Combination of:

    datetime2,
    NOT NULL, 
    StoreGeneratedPattern=Computed
    

    Works for me!

提交回复
热议问题