Generic getter and setter for value given table name, column name and id
Is there any way to write a generic getter and setter for value given table name, column name and id from entities generated from database in Entity Framework?
开发者_如何转开发The generic getter would be something like
Pseudo Code:
public T GetValue<T>(string tableName, string columnName, int id)
{
using(Entities context = new Entities())
{
return (from t in context.tableName
where t has columnName && t.id == id
select t.columnName).First();
}
}
public void SetValue<T>(string tableName, string columnName, T value, int id)
{
...
}
It's definitely possible, but you'll have to get into at least constructing expressions by hand, if not also reflection. Your way also loses some type (and typo!) safety which is one of the big advantages of proper ORM.
While it's possible to get around this by using more expressions and generics, the question remains - why do you want to do this? To be honest, these functions do not look like something I'd imagine using in practice.
精彩评论