如何将实体标记为已更改

时间:2013-12-14 07:03:15

标签: c# .net entity-framework-5

说我有一个实体类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:是否可以在不实际更改实体的情况下检索实体并将其标记为更改?

问题可能已经得到解答,但我找不到链接。

1 个答案:

答案 0 :(得分:1)

  

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

请参阅this

 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;
    }