简体   繁体   中英

Not able to edit in entity framework with asp.net/c#

 public ReturnMessage EditCategories(Category objCategory)
    {


        ReturnMessage objReturnMessage = new ReturnMessage();
        try
        {
            Category objCategoryNew = db.Categories.Where(x => x.CategoryId == objCategory.CategoryId).FirstOrDefault();
            if (objCategoryNew != null)
            {
                objCategoryNew = objCategory;
                db.SaveChanges();
                objReturnMessage.isSuccessfull = true;
                objReturnMessage.responseMessage = "Successfully updated.";
            }
            else
            {
                objReturnMessage.isSuccessfull = false;
                objReturnMessage.responseMessage = "Category not present.";
            }
        }
        catch (Exception ex)
        {
            objReturnMessage.isSuccessfull = false;
            objReturnMessage.responseMessage = ex.Message;
        }
        return objReturnMessage;

    }

Everything goes fine there are no exceptions still the data isn't getting updated. I don't know what's the issue. Please help?

The line:

objCategoryNew = objCategory;

will not work out since you change the reference objCategoryNew to objCategory , not the object itself, what you have to do is to assign each property of objCategory to objCategoryNew , something like:

objCategoryNew.Pro1 = objCategory.Pro1;
objCategoryNew.Pro2 = objCategory.Pro2;
....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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