C
C#4mo 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
Angius4mo ago
It should just be autogenerated on add By the database
sibber
sibber4mo ago
dont have to tell it to auto gen? iirc the default is only for props named Id or ClassNameId
Angius
Angius4mo ago
ye
sibber
sibber4mo ago
oh i misunderstood what you said nevermind
Angius
Angius4mo ago
.HasDefaultValueSql("getdate()") Or whatever the function for getting the date is in the database you use
Core
CoreOP4mo ago
Oh, I haven't thought of that at all.... Yes that's how it should be done Thank you
Want results from more Discord servers?
Add your server