none
Time autosave RRS feed

  • Question

  • Hi.How to save the date of update and creation of data in the database automatically? I am using codefirst.tanksfull.
    Monday, July 22, 2019 4:39 AM

All replies

  • Hi,

    this strongly depends on which database you use. In SQL Server you can set a default value GETDATE() on a datetime column to set this column with record's creation date. For Update you need to set a trigger.

    In Oracle databases you need to set a trigger for both requirements if I remember correctly.

    MySql also has a 'default' on table creation but not sure how this can be used.

    Anyway, this is no C# question. you should ask this in the corresponding DB forum.

    Monday, July 22, 2019 6:14 AM
  • Hi Ali_Lonbar,

    Thank you for posting here.

    >> How to save the date of update

    If you want to save the date of update, you could use the following code.

     DateTime time = DateTime.Now;

    I want to know if you want to save this data to database. Besides, could you tell me what is related to codefirst?

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, July 22, 2019 6:24 AM
    Moderator
  • Hello,

    If this is only for one table the following will provide the current date time, data annotations will not work in this case so this would be the way too go.

    public DateTime DateCreated
    {
       get
       {
          return this.dateCreated.HasValue
             ? this.dateCreated.Value
             : DateTime.Now;
       }
    
       set { this.dateCreated = value; }
    }
    
    private DateTime? dateCreated = null;

    Or if there are multiple tables that need this (which many times is done for a relational database)

    https://andy.mehalick.com/2014/02/06/ef6-adding-a-created-datetime-column-automatically-with-code-first-migrations/


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Monday, July 22, 2019 9:40 AM
    Moderator
  • Do it in the database using a default value for the create date and a trigger for the update date. This is pretty standard SQL database stuff as Christoph mentions. Google for 'SQL create/update column auditing' and you'll get quite a few different approaches. Most create a new audit table but if you have a lot of tables it might be better to use the existing table or a combination of create/update audit columns and additional audit detail tables.

    Michael Taylor http://www.michaeltaylorp3.net

    Monday, July 22, 2019 1:53 PM
    Moderator
  • Do it in the database using a default value for the create date and a trigger for the update date. This is pretty standard SQL database stuff as Christoph mentions. Google for 'SQL create/update column auditing' and you'll get quite a few different approaches. Most create a new audit table but if you have a lot of tables it might be better to use the existing table or a combination of create/update audit columns and additional audit detail tables.

    Michael Taylor http://www.michaeltaylorp3.net

    I agree with this if the table exists (code first from database) while not from Empty Code First Model.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Monday, July 22, 2019 2:31 PM
    Moderator