DateTime.UtcNow returns 0001-01-01T00:00:00
Hello guys I have BaseEntity and it has createdAt,modifiedAt values in it
So the problem is when i run this code it returns
But i want the values to be same when it's first created
So the problem is when i run this code it returns
"createdAt": "2023-12-27T13:57:08.3933333",
"modifiedAt": "0001-01-01T00:00:00""createdAt": "2023-12-27T13:57:08.3933333",
"modifiedAt": "0001-01-01T00:00:00"But i want the values to be same when it's first created
c#
public override async Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
{
DateTime now = DateTime.UtcNow;
foreach (var entry in ChangeTracker.Entries<BaseEntity>())
{
switch (entry.State)
{
case EntityState.Added:
if (entry.Entity.CreatedAt == DateTime.MinValue)
{
entry.Entity.CreatedAt = now;
}
entry.Entity.ModifiedAt = now;
break;
case EntityState.Modified:
entry.Entity.ModifiedAt = now;
break;
}
Console.WriteLine($"Entity Type: {entry.Entity.GetType().Name}, State: {entry.State}, CreatedAt: {entry.Entity.CreatedAt}, ModifiedAt: {entry.Entity.ModifiedAt}");
}
return await base.SaveChangesAsync(cancellationToken);
}
}c#
public override async Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
{
DateTime now = DateTime.UtcNow;
foreach (var entry in ChangeTracker.Entries<BaseEntity>())
{
switch (entry.State)
{
case EntityState.Added:
if (entry.Entity.CreatedAt == DateTime.MinValue)
{
entry.Entity.CreatedAt = now;
}
entry.Entity.ModifiedAt = now;
break;
case EntityState.Modified:
entry.Entity.ModifiedAt = now;
break;
}
Console.WriteLine($"Entity Type: {entry.Entity.GetType().Name}, State: {entry.State}, CreatedAt: {entry.Entity.CreatedAt}, ModifiedAt: {entry.Entity.ModifiedAt}");
}
return await base.SaveChangesAsync(cancellationToken);
}
}