Tutorials Menu

Generated values

Generated values are values that will be computed or generated by the database. Here are a few ways you can create such columns.

Auto incremented primary key

By convention, any primary key column of type short, int, long, or Guid is configured as an auto incrementing identity column. When a new record is inserted, a unique value is automatically generated for it.

public class School
{
    public int SchoolId { get; set; } // this is auto incremented achiving unique values for the primary key
}
public class Student
{
    public Guid Id { get; set; } // this is auto generated with unique values
}

If we want to use any other name for primary key name we must decorate the property with the [Key] attribute from System.ComponentModel.DataAnnotations. This will tell EF Core that this specific property is the primary key

    public class Supplier
    {
        [Key]
        public int SupplierKey { get; set; }
        public string Name { get; set; }
    }

As you can see in the migration script, the SupplierKey property needs an identity for the table.

Key attribute as identity in EF core
Primary Key in the table

Computed columns

Computed values are values that the database will calculate based on other columns in the same table. Databases don’t allow specifying functions like GETDATE() in computed columns.

public class Store
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Location { get; set; }
    public List<Inventory> Inventory{ get; set; }
    public string FullInfo { get; set; }
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Store>(entity => {
        entity.Property(e => e.FullInfo)
        .HasComputedColumnSql("[Name] + ', ' + [Location]");
    });
}

This is a virtual computed column. It will be calculated every time it is fetched from the database. You can see the computed column in the database as well.

Computed column results

If you want the column's value to be stored in the database, you can set the stored parameter to true in the HasComputedColumnSql method. This ensures the column is materialized and automatically recalculated whenever the data it depends on is updated.

public class Store
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Location { get; set; }
    public List<Inventory> Inventory{ get; set; }
    public string FullInfo { get; set; }
}

 protected override void OnModelCreating(ModelBuilder modelBuilder)
 {
     modelBuilder.Entity<Store>(entity => {
         entity.Property(e => e.FullInfo)
         .HasComputedColumnSql("[Name] + ', ' + [Location]", true);
     });
 }

Default values

A default value is automatically assigned to a column when no explicit value is provided in the insert statement.

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Store>().Property(c => c.OpenHours).HasDefaultValue(24); // Sets default value to 24h
}

Date/time value generation

You can call SQL fragments as a default value parameter.

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Inventory>().Property(i=>i.CreatedDate).HasDefaultValueSql("GETDATE()");
}

Unique constraint

To create a unique constraint for your database you create an index and set the isQnique value to true. This will force each record in the database to be unique.

 [Index(nameof(Name), IsUnique = true)]
 public class Supplier
 {
     [Key]
     public int SupplierKey { get; set; }
     public string Name { get; set; }
 }