EF Core HasColumnName

The HasColumnName attribute is applied to a property to specify the database column that the property should map to when the entity's property name and the database column name differ. The following example maps the Title property in the Book entity to a database column named Description in the Books table:

public class SampleContext : DbContext
    public DbSet<Book> Books { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
            .Property(b => b.Title).HasColumnName("Description");

public class Book
    public int BookId { get; set; }
    public string Title { get; set; }
    public Author Author { get; set; }

Data Annotations

The data annotations equivalent to the HasColumnName method is the Column attribute.

Date Modified: 2023-02-26

Edit this page in GitHub

Got any EF Core Question?