I'm having a strange issue when configuring my entities in Fluent API using EF Core.
All my entities have an EntityCreated
field which is a DateTime object that is set to the current DateTime upon being added to the database as a new record.
The following is my configuration code to set this default value:
builder.Property(x => x.EntityCreated).HasDefaultValue(DateTime.Now);
The problem is that every time a new record is added, instead of using the current DateTime, it will use the first DateTime used when it created the first record in the db.
I'm very confused as I've checked that this DateTime is not being set anywhere else before being commited to the db, I'm unsure if anyone else has had the same issue but I'm sort of scratching my head over this since I've also tried a few other methods such as:
1) builder.Property(x => x.EntityCreated).HasDefaultValueSql("getdate()");
2) builder.Property(x => x.EntityCreated).HasComputedColumnSql("getdate()");
Can anyone assist on this issue?
Much appreciated.
Ok I figured out the issue.
This configuration is wrong since this will set the default value to the date and time when the application started:
builder.Property(x => x.EntityCreated).HasDefaultValue(DateTime.Now);
Instead, I reverted the migration back to previous (to remove the configuration of populating the field), and used the following configuration:
builder.Property(x => x.EntityCreated).HasDefaultValueSql("getdate()");
I then created a new migration and updated the db.
This has now solved my problem.
UPDATE (MAY 9 2023):
Some may not like this approach since we are enforcing the underlying persistence to populate the field. If you're not too happy with that, then there is a simpler method:
public class Property
{
public int Id { get; set; }
public DateTime EntityCreated { get; set; }
// Other properties...
public Property()
{
EntityCreated = DateTime.Now;
}
}
public class PropertyConfiguration : IEntityTypeConfiguration<Property>
{
public void Configure(EntityTypeBuilder<Property> builder)
{
// No need for HasDefaultValueSql here.
}
}
By setting the EntityCreated
property in the constructor of the Property class, you ensure that it gets the current date and time when a new Property object is created. This way, you don't have to use the HasDefaultValueSql("getdate()")
in the PropertyConfiguration
class.