Asp.net mvc 4 更新数据库中的记录时出错,我的方法如下

Asp.net mvc 4 更新数据库中的记录时出错,我的方法如下,asp.net-mvc-4,Asp.net Mvc 4,我在这个代码中做错了什么?(我正在使用MVC4和EF) 例如:请清除此选项,以便使用MVC4 EditResponse response = new EditResponse(); try { using (WeMatchContext db = new WeMatchContext()) { B_MEMBER_REGISTER update = new B_MEMBER_REGISTER(); var output = db.B_MEMBER_R

我在这个代码中做错了什么?(我正在使用MVC4和EF) 例如:请清除此选项,以便使用MVC4

EditResponse response = new EditResponse();
try
{
    using (WeMatchContext db = new WeMatchContext())
    {
        B_MEMBER_REGISTER update = new B_MEMBER_REGISTER();
        var output = db.B_MEMBER_REGISTER.Where(x => x.MEMBER_ID == model.MEMBER_ID).FirstOrDefault();
        if(output != null )
        {
        update.FIRST_NAME = model.FIRST_NAME;
        update.LAST_NAME = model.LAST_NAME;
        update.GENDER = model.GENDER;
        update.DOB = model.DOB;

        int resultcount = db.SaveChanges();                    
        if (resultcount > 0)
        {
            response.MEMBER_ID = update.MEMBER_ID;
            response.ResultCode = 0;
            response.Message = "Updated Successfully";

        }                 

您必须将更新的数据附加到db实体。请试试这个

using (WeMatchContext db = new WeMatchContext())
{        
    var update = db.B_MEMBER_REGISTER.Where(x => x.MEMBER_ID == model.MEMBER_ID).FirstOrDefault();
    if(update != null )
    {
        update.FIRST_NAME = model.FIRST_NAME;
        update.LAST_NAME = model.LAST_NAME;
        update.GENDER = model.GENDER;
        update.DOB = model.DOB; 

        //below line of code is very important. 
        db.B_MEMBER_REGISTER.Attach(update);
        db.Entry(update).State = EntityState.Modified;

        int resultcount = db.SaveChanges();                    
        if (resultcount > 0)
        {
             response.MEMBER_ID = update.MEMBER_ID;
             response.ResultCode = 0;
             response.Message = "Updated Successfully";    
        }
   }
}

你有什么错误?代码大部分是不完整的。对不起,皮诺,这是我在堆栈溢出方面的第一个问题,我不知道如何发布和修复这个问题