EF Core column name from mapping api

前端 未结 2 1645
抹茶落季
抹茶落季 2021-01-14 19:10

In EF 6.1 the mapping API was introduced where we could finally get access to table names and column names. Getting the table name is a very nice change in EF Core, but I ha

相关标签:
2条回答
  • 2021-01-14 19:43

    This version doesn't assume SqlServer and can work as well with Npgsql provider as long as data store is relational.

    var columnNames = dbContext.Model.FindEntityType(typeof(T))
                      .GetProperties().Select(x => x.Relational().ColumnName);
    
    0 讨论(0)
  • 2021-01-14 19:50
    var columnNames = ctx.Model.FindEntityType(typeof (T))
                               .GetProperties().Select(x => x.SqlServer().ColumnName)
                               .ToList();
    

    Also

    var columnNames = ctx.Model.FindEntityType(typeof (T))
                               .GetProperties().Select(x => x.Relational().ColumnName)
                               .ToList();
    

    In EF Core 3.X, .Relational() and .SqlServer() have been replaced and you can simply use:

    var columnNames = ctx.Model.FindEntityType(typeof (T))
                               .GetProperties().Select(x => x.GetColumnName())
                               .ToList();
    
    0 讨论(0)
提交回复
热议问题