Share Coding

Tutorials, Problems, Stuffs …

Category Archives: Entity Framework

Entity Framework Update table without primary Key (II)

The property ‘xxx’ is part of the object’s key information and cannot be modified

UserRight dboUserRight = DBModel.UserRights.FirstOrDefault(x => x.UserID == id 
                                                             && x.PageID == pid); 
dboUserRight.CanWrite = newValue; 
DBModel.SaveChanges();

Method 1: Replace old record

UserRight updatedUserRight = new UserRight();
updatedUserRight.CanWrite = newValue;
updatedUserRight.OtherProperty = xxxx;
DBModel.UserRights.Add(updatedUserRight);
DBModel.UserRights.Remove(dboUserRight);
DBModel.SaveChanges();
Advertisements

Entity Framework Update table without primary Key

Exception throw when try to call DBModel.SaveChanges()

System.Data.Entity.Infrastructure.DbUpdateException: Unable to update the EntitySet ‘role_app_right’ because it has a DefiningQuery and no <InsertFunction> element exists in the <ModificationFunctionMapping> element to support the current operation. —> System.Data.Entity.Core.UpdateException: Unable to update the EntitySet ‘role_app_right’ because it has a DefiningQuery and no <InsertFunction> element exists in the <ModificationFunctionMapping> element to support the current operation.

  1. Right click on the edmx file, select Open with… > XML Text editor
  2. Find the EntitySet in edmx:StorageModels > EntityContainer
  3. Remove the DefiningQuery entirely
  4. Rename [store:Schema=”dbo”] to [Schema=”dbo”]

123

<EntitySet Name=“role_app_right” EntityType=“Self.role_app_right” store:Type=“Tables” Schema=“dbo” />