Update a single property of a record in Entity Framework Code First

后端 未结 3 910
迷失自我
迷失自我 2020-12-30 03:33

How can I update a single property of a record without retrieving it first? I\'m asking in the context of EF Code First 4.1

Says I have a class User

3条回答
  •  心在旅途
    2020-12-30 04:31

    You can try a sort of hack:
    context.Database.ExecuteSqlCommand("update [dbo].[Users] set [LastActivity] = @p1 where [Id] = @p2",
    new System.Data.SqlClient.SqlParameter("p1", DateTime.Now),
    new System.Data.SqlClient.SqlParameter("p2", id));

提交回复
热议问题