简体   繁体   English

MVC模型 - 如何在DB中更新一行?

[英]MVC model - how to update one row in DB?

I am new to MVC. 我是MVC的新手。 I am adding into the database OK with this code: 我使用以下代码添加到数据库中:

    [HttpPost]
    [Authorize]
    public ActionResult Add(CourseModel course)
    {
        course.UserID = (Guid)Membership.GetUser().ProviderUserKey;

        _db.Class.Add(course);
        _db.SaveChanges();

        return View();
    }

But how do I correctly update a 'course' if I already have it in the DB, but the user updated one field?. 但是如果我已经在数据库中拥有它,但如果用户更新了一个字段,我如何才能正确更新“课程”? I can delete it, and then read the updated one and that works OK but I'm pretty sure that is not the correct way of doing it. 我可以删除它,然后阅读更新的一个,这是正常的,但我很确定这不是正确的方法。

Thank you. 谢谢。

Assuming you're using EF, to update a record in database. 假设您正在使用EF,以更新数据库中的记录。 Using the following code 使用以下代码

_db.Entry<Course>(course).State = System.Data.EntityState.Modified;
_db.SaveChanges();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM