locked
Simple Save Operation - Getting System.Data.UpdateException RRS feed

  • Question

  • I'm getting the following error when I try to insert a row.    Can anyone help me understand the error?



    ERROR
    <?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>
    <error xmlns=\"http://schemas.microsoft.com/ado/2007/08/dataservices/metadata
    <message xml:lang=\"en-US\">An error occurred while processing this request.</message>
    <innererror xmlns=\"xmlns\">
    <message>Entities in 'MedRecordSecureEntities.FacilityDigitalFiles' participate in the 'FK_FacilityDigitalFiles_DigitalFileTypeLib' relationship. 0 related 'DigitalFileTypeLib' were found. 1 'DigitalFileTypeLib' is expected.</message>
    <type>System.Data.UpdateException</type>

    <stacktrace> at System.Data.Mapping.Update.Internal.UpdateTranslator.RelationshipConstraintValidator.ValidateConstraints()&#xD;\n at System.Data.Mapping.Update.Internal.UpdateTranslator.ProduceCommands()&#xD;\n at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter)&#xD;\n at System.Data.EntityClient.EntityAdapter.Update(IEntityStateManager entityCache)&#xD;\n at System.Data.Objects.ObjectContext.SaveChanges(Boolean acceptChangesDuringSave) &#xD;\n at System.Data.Objects.ObjectContext.SaveChanges()&#xD;\n at System.Data.Services.Providers.ObjectContextServiceProvider.SaveChanges()&#xD;\n at System.Data.Services.DataService` 1.BatchDataService.HandleBatchContent(Stream responseStream)
    </stacktrace>

    </innererror></error>\r\n

    CODE:
     try
    {

    MedicalRecordSecureSvc.FacilityDigitalFiles facilityFiles = new MedRecordSecure.MedicalRecordSecureSvc.FacilityDigitalFiles();
    facilityFiles.CreatedBy = Guid.Empty;
    facilityFiles.CreatedOn = DateTime.Now;

    facilityFiles.SavedFileName = e.SelectedFile.Name;
    facilityFiles.UploadedBy = Guid.Empty;
    facilityFiles.UploadFileName = e.SelectedFile.Name;

    facilityFiles.DigitalFileTypeLib = this.listDigitalFileType[0];
    this.listDigitalFileType[0].FacilityDigitalFiles.Add(facilityFiles);

    this.entities.AddToFacilityDigitalFiles(facilityFiles);

    this.entities.BeginSaveChanges(SaveChangesOptions.Batch, this.ChangesSaved, null);
    }
    catch (System.Exception rExc)
    {
    MessageBox.Show(rExc.Message);
    }
     In the code the entities object is an instance of the entity data model.
     private void ChangesSaved(System.IAsyncResult ar)
    {
    DataServiceResponse response = null;
    try
    {
    response = this.entities.EndSaveChanges(ar);
    MessageBox.Show(ar.IsCompleted.ToString());
    }
    catch (Exception rExc)
    {
    MessageBox.Show(rExc.Message);
    }
    }

    The FacilityDigitalFiles table has the following schema
    FacilityDigitalFileID
    UploadedBy
    UploadedOn
    DigitalFileTypeID

    The FacilityDigitalFileTypeLib has the follwoing schema
    DigitalFileTypeID
    ShortName
    LongName


    The Entity Data Model generates the relationship as expected.

    I have tried many different attempts at organizing the code for the save.  

    I'm clueless right now.
    Saturday, July 18, 2009 3:56 AM

Answers