EF4 Code First: how to update specific fields only
How do I update only certain fields on an entity?
I have a User entity like so:
public class User
{
public string UserId { get; set; }
public string PasswordHash { get; set; }
public bool IsDisabled { get; set; }
public DateTime AccessExpiryDate { get; set; }
public bool MustChangePassword { get; set; }
public DateTime DateCreated { get; set; }
public DateTime LastActivity { get; set; }
}
So if for example, I want to update the user entity, but do not want to change the user password, how do I do that?
Currently, I'm using the following code to update entities:
using (var _cnt = new STQContext())
{
_cnt.Entry<Item>(item).State = System.Data.EntityState.Modified;
_cnt.SaveChanges();
return;
}
尝试这个:
using (var _cnt = new STQContext())
{
_cnt.Users.Attach(user);
_cnt.Entry<User>(user).Property(u => u.PasswordHash).IsModified = true;
_cnt.SaveChanges();
}
链接地址: http://www.djcxy.com/p/51530.html
上一篇: Interix是否实现fork()?