Reputation: 7577
The postgres function Now() can be used to set a created time for any row. It is defined as this using postgresql:
CREATE TABLE student
(
stud_id serial PRIMARY KEY,
stud_name Text NOT NULL,
created_date TIMESTAMPTZ DEFAULT Now()
);
The problem is. I am using the code first approach with entity models. How can I then achieve the same functionality without adding a lot of extra code?
My class look like:
public class student
{
public int stud_id { get; set; }
public string stud_name { get; set; }
public DateTime created_date { get; set; }
}
Upvotes: 0
Views: 839
Reputation: 143003
HasDefaultValueSql
should work fine and I think it is not a lot of extra code:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<student>()
.Property(b => b.created_date)
.HasDefaultValueSql("NOW()");
}
Upvotes: 2