Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set created date and Modified Date to enitites in DB first approach

Tags:

In every SQL table in our application we have "CreatedDate" and "ModifiedDate" column. We are using DB first approach. When i save the data, i want these two column automatically populated. One approach is to have Default Value as getdate() on the SQL Table Column itself. So thats going to solve the problem partially. Meaning it will set the CreatedDate & ModifiedDate when entity is new. However when i am editing/updating the entity i want only ModifiedDate to be updated.
There are lot of articles doing it using Code first approach. But we are using DB first approach.
What are my options here?

like image 675
LP13 Avatar asked May 17 '16 20:05

LP13


People also ask

How do I change the default value for EF core?

In EF core released 27th June 2016 you can use fluent API for setting default value. Go to ApplicationDbContext class, find/create the method name OnModelCreating and add the following fluent API.

How soft delete is implemented in Entity Framework?

The Soft Delete feature allows you to flag entities as deleted (Soft Delete) instead of deleting them physically (Hard Delete). The soft delete feature can be achieved by using the 'IEFSoftDelete' interface. By default, this interface is always added to the manager.

What is Entity Framework Core?

Entity Framework (EF) Core is a lightweight, extensible, open source and cross-platform version of the popular Entity Framework data access technology. EF Core can serve as an object-relational mapper (O/RM), which: Enables . NET developers to work with a database using . NET objects.

How to set the created and updated date of an entity?

It involves creating an interface for both the updated and created dates. Then you can add this to your entities. public interface IDateCreated { DateTime DateCreated { get; set; } } Then we need one for the updated date. Note that this is a nullable date. If an entity is never updated, then we need never set the date.

What is the difference between created and modified date in tsfdb?

The value of the created date column will be set when the record is created. The value of the modified date will be set via a trigger that will be created on the table. In the example below I’m creating a new table called dbo.recipe in the TSFDB database.

How do I set the value of the created and modified date?

The value of the created date column will be set when the record is created. The value of the modified date will be set via a trigger that will be created on the table. In the example below I’m creating a new table called dbo.recipe in the TSFDB database. The table contain 16 columns, most of which are reserved for ingredients.

What is the difference between the created_date and modified_date columns?

The created_date column is configured to insert the current date into the column when the record is created. The modified_date column will remain NULL when the record is created.


1 Answers

If you'd like to override OnSave you have to override all save methods.In EF core 2.1 you can use better solution with ChangeTracked and events. For example:

You can create interface or base class like example below:

public interface IUpdateable  {      DateTime ModificationDate{ get; set; } }  public class SampleEntry : IUpdateable {     public int Id { get; set; }     public DateTime ModificationDate { get; set; } }  

Then on context creation add event to Change tracker:

context.ChangeTracker.StateChanged += context.ChangeTracker_StateChanged; 

And method:

private void ChangeTracker_StateChanged(object sender, Microsoft.EntityFrameworkCore.ChangeTracking.EntityStateChangedEventArgs e)     {         if(e.Entry.Entity is IUpdateable && e.Entry.State == EntityState.Modified)         {             var entry = ((IUpdateable)e.Entry.Entity);             entry.ModificationDate = DateTime.Now;         }     } 

It's easier and you don't have to override all methods.

like image 60
kjppster Avatar answered Oct 16 '22 13:10

kjppster