none
The property 'User_Id' is part of the object's key information and cannot be modified. RRS feed

  • Question

  • I am trying to replace a user  ID in a SQL Table column with another user ID but getting the Error "The property 'User_Id' is part of the object's key information and cannot be modified. "  Below is my code.

                using (var context = new Model())
                {
                    var model = context.HeaderRoles
                        .Where(f => f.User_Id.Equals(OldUserId))
                        .ToList();
                    model.ForEach(f => f.User_Id = NewUserId);
                    foreach (var headerRole in model)
                        context.HeaderRoles.Add(headerRole);
                    context.SaveChanges();
    
                    return Json(model, JsonRequestBehavior.AllowGet);
                }


    Ebenezer

    Sunday, July 16, 2017 5:57 PM

Answers

  • I am trying to replace a user  ID in a SQL Table column with another user ID

    If the user-id is the primary-key for the table, then no, the DB engine is not going to allow the primary-key of the table to be changed.

    • Marked as answer by denkyira Sunday, July 16, 2017 9:15 PM
    Sunday, July 16, 2017 6:16 PM

All replies

  • I am trying to replace a user  ID in a SQL Table column with another user ID

    If the user-id is the primary-key for the table, then no, the DB engine is not going to allow the primary-key of the table to be changed.

    • Marked as answer by denkyira Sunday, July 16, 2017 9:15 PM
    Sunday, July 16, 2017 6:16 PM
  • user-id is not the primary for the table "HeaderRoles" but a  foreign key. Attached is my table


    Ebenezer

    Sunday, July 16, 2017 7:06 PM
  • Thanks got it fixed.

    Ebenezer

    Sunday, July 16, 2017 9:15 PM