如何将实体标记为已更改

本文关键字:记为 实体 | 更新日期: 2024-09-21 06:01:52

假设我有一个实体类user。现在在那节课上,我想做:

partial class user{
    public void save()
    {
       using (ent e = new ent())
       {
           var res = (from u in e.user where u.name == name select u).first();
           res = someOtherUserObject;
           context.SaveChanges();
       }
    }
}
  • Q1:这可能吗
  • Q2:是否可以在不实际更改的情况下检索实体并将其标记为已更改

这个问题可能已经回答了,但我找不到链接。

如何将实体标记为已更改

Q2:是否可以在不实际更改的情况下检索实体并将其标记为已更改?

请参阅:

 using (var context = new BloggingContext())
    {
        var blog = context.Blogs.Find(1);
        context.Entry(blog).Property(u => u.Name).IsModified = true;
        // Use a string for the property name
        context.Entry(blog).Property("Name").IsModified = true;
    }