Vorrei ottenere "Nome colonna, Valore colonna" in un elenco di tuple array con framework di entità .
Ho questo:
var colNames = typeof(tbl_ObisSchema).GetProperties().Select(a => a.Name).ToList();
per ottenere tutti i nomi delle colonne
using (var dbContext = new db_ReadyEngine_MSSQL())
{
var colNames = typeof(tbl_ObisSchema).GetProperties().Select(a => a.Name).ToList();
List<Tuple<string, string>> list = new List<Tuple<string, string>>();
list.Add(Tuple.Create(ColumnName, ColumnValue));
}
Come fatto? Molte grazie...
Una colonna da sola non è abbastanza; dovrai leggere i valori delle colonne da un'entità specifica.
Una volta che si dispone di un'entità, è possibile utilizzare il metodo DbContext.Entry
per ottenere l'istanza DbEntityEntry
per tale entità. La proprietà CurrentValues
ti darà quindi accesso ai valori di proprietà correnti per quell'entità.
using (var dbContext = new db_ReadyEngine_MSSQL())
{
var entity = dbContext.Set<tbl_ObisSchema>().Find(thePrimaryKeyToFind);
if (entity == null) throw new InvalidOperationException("Record not found");
var entry = dbContext.Entry(entity);
var currentPropertyValues = entry.CurrentValues;
List<Tuple<string, object>> list = currentPropertyValues.PropertyNames
.Select(name => Tuple.Create(name, currentPropertyValues[name]))
.ToList();
// Do something with the list...
}