实体框架在c#中按id更新记录

时间:2015-05-07 07:40:13

标签: c# entity-framework

我是新用的实体框架

如何将此转换为实体框架c#
query = "update cachieroperation set last_used = getdate()+'0:8:0' where id = 14"

2 个答案:

答案 0 :(得分:8)

应该是(我不知道你的类叫什么):

using(var context = new SomeEntities())
{
     CarrierOperation carrierOperation = context.CarrierOperations.SingleOrDefault(co=> co.id == 14);
     if(carrierOperation != null)
     {
         carrierOperation.last_used = DateTime.Now.AddMinutes(8);
         context.SaveChanges();
     }
}

答案 1 :(得分:0)

您可以使用ExecuteQuery直接对数据库执行查询

string query = "update cachieroperation set last_used = getdate()+'0:8:0' where id = 14";
context.ExecuteQuery<cachieroperation>(query );  

有关详细信息,请尝试此链接ExecuteQuery