Home > Enterprise >  How to Get Column Name From Entity Type Configuration With Property Name In EF Core
How to Get Column Name From Entity Type Configuration With Property Name In EF Core

Time:01-31

I have such EntityTypeConfiguration class.

public class DummyTypeConfiguration : IEntityTypeConfiguration<DummyType>
{
  public void Configure(EntityTypeBuilder<DummyType> builder)
  {
      builder.HasNoKey();
      builder.Property(p => p.SecretId).HasColumnName("secretID");
      ...
  }
}

I want to retrieve the column name: "secretID" by using the property DummyType.SecretId as such:

var columnName = ...(DummyType.SecretId);

CodePudding user response:

EF Core 6: That can be done using the code below:

var entityType = _dataContext.Model.FindEntityType(typeof(DummyType));
var property = entityType?.GetProperty(nameof(DummyType.SecretId));
var columnName = property?.GetColumnName();

The _dataContext variable represents object instance of DbContext class.

  • Related