How to manage GetDate() with Entity Framework

前端 未结 5 1243
一生所求
一生所求 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!

    0 讨论(0)
  • 2021-02-07 04:33

    Changing type of DateCreated to datetime2 might solve the problem.

    datetime 2007-05-08 12:35:29.123

    datetime2 2007-05-08 12:35:29. 12345

    Ref: http://technet.microsoft.com/en-us/library/bb677335.aspx67

    0 讨论(0)
  • 2021-02-07 04:40

    Focusing on the fact that I do not want change the database, since it is an application problem and I expect them to solve this problem one day, my solution (that is totally possible in my case) is to create a partial class of the model to correct the problem on constructor:

    public partial class Log
    {
        public Log()
        {
            this.Date = DateTime.Now;
        }
    }
    

    This works for me because:

    • I create the model on the moment I send it to database.
    • I use CLOUD for these services, the datetime must be the same in both Application and Database servers!

    Don't forget that the namespace needs to match the Model namespace or partial should not list properties (and should no be partial as well ;])!

    0 讨论(0)
  • 2021-02-07 04:45

    Here is a working workaround:

    1) Change the column to datetime2 as mentioned elsewhere. This fixes the conversion error.

    2) Add a trigger that sets DateCreated to getdate();

    CREATE TRIGGER [TR_AS_ChangeTime] ON [AS_ApplicationSession] 
      AFTER INSERT,UPDATE AS
    BEGIN
    
      SET NOCOUNT ON;
      UPDATE AS_ApplicationSession 
      SET AS_ChangeTime = getdate() 
      WHERE AS_Id IN(SELECT AS_ID FROM INSERTED)
    
    END
    

    3) If neccessary, set

    p.DateCreated = DateTime.MinValue;
    

    just to initialize it.

    4) If you need the DateCreated from the database, add

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

    just after

    context.SaveChanges();
    
    0 讨论(0)
  • 2021-02-07 04:50

    You have to manually edit the edmx xml and set your SSDL StoreGeneratedPattern attributes to identity or computed. But whenever you update your edmx via the designer your changes will get overwritten.

    This is a known issue. Please see the following links for more details:

    Microsoft Connect Ticket

    Using a GUID as an EntityKey in Entity Framework 4

    0 讨论(0)
提交回复
热议问题