Jaa


Missing [RoundtripOriginal()] for key properties in RIA auto-generated code causing RIA entity update failure

Issue: After upgrading Silverlight application from Silverlight 4 to Silverlight 5, updating data via RIA service throws Error "The property '<xxxxxx - a key property name>_id' is part of the object's key information and cannot be modified."

Example Call Stack:

Message="The property 'KeyName_id' is part of the object's key information and cannot be modified. "
Source=System.Data.Entity
   at System.Data.Objects.EntityEntry.CompareKeyProperties(Object changed)
   at System.Data.Objects.EntityEntry.ApplyOriginalValuesInternal(IEntityWrapper wrappedOriginalEntity)
   at System.Data.Objects.EntityEntry.ApplyOriginalValues(Object originalEntity)
   at System.ServiceModel.DomainServices.EntityFramework.ObjectContextUtilities.AttachAsModifiedInternal[T](T current, T original, ObjectContext objectContext)
   at System.ServiceModel.DomainServices.EntityFramework.ObjectContextExtensions.AttachAsModified[T](ObjectSet`1 objectSet, T current, T original)
   at xxxxxx. ... xxx_DomainServiceClass.Update...
   at Updatexxx...
   at System.ServiceModel.DomainServices.Server.ReflectionDomainServiceDescriptionProvider.ReflectionDomainOperationEntry.Invoke(DomainService domainService, Object[] parameters)
   at System.ServiceModel.DomainServices.Server.DomainService.InvokeDomainOperationEntry(DomainOperationEntry domainOperationEntry, Object[] parameters, ChangeSetEntry operation)
   at System.ServiceModel.DomainServices.Server.DomainService.InvokeCudOperations()
   at System.ServiceModel.DomainServices.Server.DomainService.ExecuteChangeSet()
   at System.ServiceModel.DomainServices.Server.DomainService.Submit(ChangeSet changeSet)

Root Cause: “[RoundtripOriginal()]” is not generated by RIA Service in the auto-generated code file: <SLProjectName>.Web.g.cs.

Troubleshooting tips: This type of error happened only after Silverlight version upgrade usually is caused by referenced assemblies mismatch. The best way is to isolate the issue down by removing references one by one and related code.
In this particular case, the issue is caused by a specific version System.Windows.dll (5.0.61118.0) referenced locally in the Web Project that hosts the RIA Service .

Resolution: remove the System.Windows.dll reference from the RIA service web proejct and make sure it is deleted form the local bin folder. After this, RIA service generated [RoundtripOriginal()] for key properties as expected.

For Example:

 [DataMember()]
        [Editable(false, AllowInitialValue=true)]
        [Key()]
        [RoundtripOriginal()]
        public Guid ApplicationId
        {
            get
            {
                return this._applicationId;
            }
            set
            {
                if ((this._applicationId != value))
                {
                    this.OnApplicationIdChanging(value);
                    this.ValidateProperty("ApplicationId", value);
                    this._applicationId = value;
                    this.RaisePropertyChanged("ApplicationId");
                    this.OnApplicationIdChanged();
                }
            }
        }

If the mismatching System.Windows.dll reference is added back, we will see warning message in the <SLProjectName>.Web.g.cs file (a hidden file to show a hidden file in VS.net,
you will need to select the project in Solution Explorer, then click the "show all files" icon on the top).

        // The following attributes were not generated:
        //
        // - The attribute 'System.ComponentModel.DataAnnotations.RoundtripOriginalAttribute' is not visible in the client project 'YourSilverlightProjectName'. Are you missing an assembly reference?
        // [RoundtripOriginalAttribute()]
        //

This warning message will be in <SLProjectName>.Web.g.cs file for each key property.

Once the mismatch reference is removed and clean solution then rebuild solution, [RoundtripOriginal()] will be generated. After [RoundtripOriginal()] is generated for each key property in <SLProjectName>.Web.g.cs, the error "The property '<xxxxxx - a key property name>_id' is part of the object's key information and cannot be modified." with exception call stack above will be gone.

Comments

  • Anonymous
    March 31, 2014
    Hi there folks, I Stumbled upon the same issue with the WCF-RIA service. I'm using entity framework with WCF-RIA framework to access my MSSQL database. But somehow I'm unable to update the records in the database. Insert, delete and select works, but I'm getting an error below for update: "The property 'id' is part of the object's key information and cannot be modified. and also it shows a warning in the error list window: The attribute 'System.ComponentModel.DataAnnotations.RoundtripOriginalAttribute' requires a reference to System.ComponentModel.DataAnnotations in the client project 'InventoryApplications'. Skipping generation of attribute. Please add a reference to System.ComponentModel.DataAnnotations to ensure generation of the attribute. According to the documentation this warning is responsible for the update functions in the entity framework. then I stumbled upon this solution in this website Solutions But the problem is I cannot delete System.windows.dll reference without generating more errors. Frequent Forum answers:
  1. I did not entered primary key in the table. ==> currently there is a primary key and the update is not touching primary key field at all.
  2. copy and make new solutions. ==> The project is going to launch soon. cannot do this too.