C
C#5mo ago
Core

Best practices: Where should I assign value to an EF model DateTime property

Hello, I would need to assign DateTime.UtcNow to a property called CreatedAt. Should this be done in the model, business logic layer or repository layer?
c#
public class Entity
{
public long Id { get; set; }
public DateTime CreatedAt { get; set; }
}
c#
public class Entity
{
public long Id { get; set; }
public DateTime CreatedAt { get; set; }
}
6 Replies
Angius
Angius5mo ago
It should just be autogenerated on add By the database
sibber
sibber5mo ago
dont have to tell it to auto gen? iirc the default is only for props named Id or ClassNameId
Angius
Angius5mo ago
ye
sibber
sibber5mo ago
oh i misunderstood what you said nevermind
Angius
Angius5mo ago
.HasDefaultValueSql("getdate()") Or whatever the function for getting the date is in the database you use
Core
CoreOP5mo ago
Oh, I haven't thought of that at all.... Yes that's how it should be done Thank you

Did you find this page helpful?