How to Specify Entity Framework Core Table Mapping?

  1. To specify the name of the database table, you can use an attribute or the fluent API:

    Using Attributes:

     [Table("MyAccountsTable")]
     public class Account
     {
          public string PasswordHash { get; set; }
     }
    

    Using Fluent API:

     public class YourContext : DbContext
     {
         protected override void OnModelCreating(ModelBuilder builder)
         {
             builder.Entity<Language>(entity => {
                 entity.ToTable("MyAccountsTable");
             });
         }
     }
    
  2. To name your columns manually, it’s very similar and you can use an attribute or the fluent API:

    Using Attributes:

     public class Account
     {
         [Column("MyPasswordHashColumn")]
         public string PasswordHash { get; set; }
    
     }
    

    Using Fluent API:

     public class YourContext : DbContext
     {
         protected override void OnModelCreating(ModelBuilder builder)
         {
             builder.Entity<Language>(x => x
                 .ToTable("MyAccountsTable")
                 .Property(entity => entity.PasswordHash)
                     .HasColumnName("MyPasswordHashColumn")
             );
         }
     }
    

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)