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?
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.
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.
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.
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.
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 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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With