dotnet / efcore

EF Core is a modern object-database mapper for .NET. It supports LINQ queries, change tracking, updates, and schema migrations.
https://docs.microsoft.com/ef/
MIT License
13.78k stars 3.19k forks source link

Is EF has a default value in columns annotation #34732

Closed WarlockNDF closed 1 month ago

WarlockNDF commented 1 month ago

Is it Possible To declare the SQL Default Value in the [Column] annotation so the ef migrates can automatic generate default value

Include your code

public class TimeStamp
{

    [Column("created_at", DefaultValue =  "now()")]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public DateTime? Inserted { get; set; }

    [Column("updated_at", DefaultValue =  "now()")]
    [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
    public DateTime? LastUpdated { get; set; }

}

Include provider and version information

EF Core version: Database provider: (e.g. Npgsql.EntityFrameworkCore.PostgreSQL 8.0.4) Target framework: (e.g. .NET 9) Operating system: Window 11 IDE: Rider

roji commented 1 month ago

That's covered in the documentation. This can be done via the Fluent API, but not via attributes.