How to Specify Entity Framework Core Table Mapping?

后端 未结 1 870
轻奢々
轻奢々 2020-12-14 18:06

I\'ve made a simple Entity Framework ASP Core Application which works but I do not know why:

I\'ve made a context like this:

public class AstootCont         


        
相关标签:
1条回答
  • 2020-12-14 18:42
    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)
                      .HasField("MyPasswordHashColumn")
              );
          }
      }
      
    0 讨论(0)
提交回复
热议问题