EF Core column name from api mapping

In EF 6.1, a mapping API was introduced in which we could finally access the table names and column names. Getting the table name is a very nice change in EF Core, but I have not yet discovered how to get the column names.

For anyone interested here, I got the table name in the latest version (RC1)

context.Model.FindEntityType(typeof(T)).SqlServer().TableName 

What is the current method for getting column names or not yet available?

+3
entity-framework-core
source share
2 answers
 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() were replaced, and you can simply use:

 var columnNames = ctx.Model.FindEntityType(typeof (T)) .GetProperties().Select(x => x.GetColumnName()) .ToList(); 
+6
source share

This version does not imply SqlServer and can work with the Npgsql provider as long as the data store is relational.

 var columnNames = dbContext.Model.FindEntityType(typeof(T)) .GetProperties().Select(x => x.Relational().ColumnName); 
+3
source share

All Articles