If I'm using EF 5 and Database first to generate a .edmx model of my database, how do I get a list of an entity's columns?
using (var db = new ProjectNameContext())
{
// string[] colNames = db.Users.
}
What I'm looking for is colNames[0] == "Id", colNames[1] == "FirstName", etc.
How about:
var names = typeof(User).GetProperties()
.Select(property => property.Name)
.ToArray();
Of course, this can be used for any type, not just an EF table.
var res = typeof(TableName).GetProperties()
.Select(property => property.Name)
.ToArray();
OR
var res = dbContext.Model.FindEntityType(typeof(TableName))
.GetProperties().Select(x => x.Relational().ColumnName)
.ToList();
var index = 0;
var propertyInfo = res[index].PropertyInfo;
var columnName = res[index].Relational().ColumnName;
var propertyName = propertyInfo.Name;
var propertyValue = propertyInfo.GetValue(sourceObject); // NEED OBJECT TO GET VALUE
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With