EntityFramework附加实体

//0.0创建修改的 实体对象
Models.BlogArticle model = new BlogArticle();
model.AId = 12;
model.ATitle = "新的数据";
model.AContent = "新的数据~~~~~";

//0.1添加到EF管理容器中,并获取 实体对象 的伪包装类对象
DbEntityEntry<Models.BlogArticle> entry = db.Entry<Models.BlogArticle>(model);

//**如果使用 Entry 附加 实体对象到数据容器中,则需要手动 设置 实体包装类的对象 的 状态为 Unchanged**
//**如果使用 Attach 就不需要这句
entry.State = System.Data.EntityState.Unchanged;

//0.2标识 实体对象 某些属性 已经被修改了
entry.Property("ATitle").IsModified = true;
entry.Property("AContent").IsModified = true;

//3.跟新到数据库
db.SaveChanges();

以上代码转载自:http://www.cnblogs.com/jameszou/archive/2013/03/12/2956281.html

以下代码是我自己写的:

        private List<int> indexs = new List<int>();
        private List<LL.Models.SysMenu> datasource;       
 private void EditBTN_Click(object sender, EventArgs e)
        {
            var model = datasource[indexs[0]];
            var db = new LL.Models.LLEntities();
            db.Entry(model).State = EntityState.Modified;
            db.SaveChanges();
            db.Dispose();
        }

 

posted @ 2013-07-23 20:29  liulun  阅读(1563)  评论(0编辑  收藏  举报