I have dbContext and table name. I would like to receive all properties names from class that represent that table. Important thing - properies names in class, cause in class some properies are corrected with Column
attribute to make class consistent with db entity.
public class MyTable
{
public int Id { get; set;}
public string Desc { get; set;}
[Column("DbProp")]
public int ClassProp{ get; set;}
}
Desired output: ["Id", "Desc", "ClassProp"]
I can get all table fields names from context metadata, but don't know how to "connect" them with Column
attribute.
public static IEnumerable<SelectListItem> GetTableColumns(DbContext context, string table)
{
var metadata = ((IObjectContextAdapter)context).ObjectContext.MetadataWorkspace;
var tableType = metadata.GetItemCollection(DataSpace.SSpace)
.GetItems<EntityContainer>()
.Single()
.BaseEntitySets.OfType<EntitySet>()
.FirstOrDefault(s => (!s.MetadataProperties.Contains("Type") || (s.MetadataProperties["Type"].ToString() == "Tables"))
&& string.Equals(s.Table ?? s.Name, table, StringComparison.OrdinalIgnoreCase));
return tableType?.ElementType.Properties.Select(f => new SelectListItem
{
Text = f.Name,
Value = f.Name
});
}
Aucun commentaire:
Enregistrer un commentaire