using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Metadata.Builders; public class Product { public int Id { get; set; } public string Name { get; set; } public decimal Price { get; set; } } public class MyDbContext : DbContext { public DbSetProducts { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { EntityTypeBuilder productEntityTypeBuilder = modelBuilder.Entity (); productEntityTypeBuilder.HasKey(p => p.Id); } }
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Metadata.Builders; public class Product { public int Id { get; set; } public string Name { get; set; } public decimal Price { get; set; } public DateTime CreatedDate { get; set; } = DateTime.UtcNow; } public class MyDbContext : DbContext { public DbSetIn this example, we define a Product entity that includes a property for CreatedDate with a default value of the current UTC time. In the OnModelCreating method, we use EntityTypeBuilder to configure the CreatedDate property and specify its default value. The package library that EntityTypeBuilder is included in is Microsoft.EntityFrameworkCore.Products { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { EntityTypeBuilder productEntityTypeBuilder = modelBuilder.Entity (); productEntityTypeBuilder.Property(p => p.CreatedDate) .HasDefaultValue(DateTime.UtcNow); } }