如何使用Entity Framework代码首先更新一行? [英] How to update a row using Entity Framework code first?

查看:85
本文介绍了如何使用Entity Framework代码首先更新一行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何更新数据库中的一行?没有更新方法,如果我使用add并且主键标识已经存在,我会收到异常。请尽可能提供一个例子。

解决方案

最简单的方法是:



< (2)更新属性



(3)在上下文中调用SaveChanges()。



例如

  var student = context.Students.Find(42); 

student.Description =更新;

context.SaveChanges();


How should I go about updating a row in the database? There is no update method, and if I use add and the primary key id already exists, I get an exception. Please provide an example if possible.

解决方案

The easiest way is:

(1) retrieve existing row using pk.

(2) update properties.

(3) call SaveChanges() on context.

e.g.

        var student = context.Students.Find(42);

        student.Description = "updated";

        context.SaveChanges();

这篇关于如何使用Entity Framework代码首先更新一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆