How to manage GetDate() with Entity Framework

前端 未结 5 1239
一生所求
一生所求 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: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 ;])!

提交回复
热议问题