我想更新一个动态的列。在我的员工表中,我有各种列,要更新的列取决于选择,因此它是动态的。
在员工表中,我有5列 - Id,name,desig,depart,Sal
所以如果我必须更新设计栏: -
string columnName = "desig"; // This comes as a parameter. This is a dynamic Value. This is just an example.
var data = ctx.tblEmp.Where(e => e.Id == model.Id).Select(e => e).SingleOrDefault();
data.desig = 'NewValue';
ctx.tblEmp.Attach(data);
...
ctx.SaveChanges();
答案 0 :(得分:1)
由于只有5个常量列,我会选择简单的解决方案:
var data = ctx.tblEmp.SingleOrDefault(e => e.Id == model.Id);
switch (columnName)
{
case "Id":
data.id = newValue; //newValue should have a correct type.
break;
case "name":
data.name = newValue;
break;
case "desig":
data.desig = newValue;
break;
case "depart":
data.depart = newValue;
break;
case "Sal":
data.Sal = newValue;
break;
}