开发者

Generic getter and setter for value given table name, column name and id

开发者 https://www.devze.com 2023-03-15 23:37 出处:网络
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?

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号