Ошибка «Идентификатор свойства не может быть изменена» при попытке обновить другое свойство - PullRequest
1 голос
/ 24 сентября 2019

Этот вопрос задавался ранее, и каждый раз ответом было то, что код обновлял свойство Id объекта.Я не могу видеть, как мой код изменяет это свойство.

System.InvalidOperationException: свойство 'Id' является частью ключевой информации объекта и не может быть изменено.at System.Data.Entity.Core.Objects.EntityEntry.DetectChangesInProperty (порядковый номер Int32, логическое deteOnlyComplexProperties, логическое обнаружение только)

Вышеприведенная ошибка выдается в первой строке, где я устанавливаю свои объекты AttemptСвойство IsModified = true видно ниже.

private static void SaveDocumentCreationToDatabase(Document document)
{
    using (MyEntities context = new MyEntities ())
    {
        context.Database.Log += (x) => dbSaveLog.Debug($"Document Id: {document.Id}. Message:\r\n{x}");

        context.Documents.Attach(document); // state = unchanged

        // Set document modified properties
        context.Entry(document).Property(u => u.Attempt).IsModified = true; // ERROR THROWN HERE
        context.Entry(document).Property(u => u.Result).IsModified = true;
        context.Entry(document).Property(u => u.NewId).IsModified = true;
        context.Entry(document).Property(u => u.PropertiesUploaded).IsModified = true;

        // modify some of the navigation properties ... etc

        // Debug 
        // Inspect Change Tracking on this context
        // Output all tracked entities that are added or modified, their key, and their modified properties
        string logOfModifiedEntities = GetAllModifiedEnitiesFromContext(document, context);                
        dbSaveLog.Debug($"{logOfModifiedEntities}");

        // End debug

        context.SaveChanges();
    }
}

Объект документа выглядит следующим образом:

public class Document
{
    public int Id { get; set; }

    // Previous system metadata  
    public string DocumentKey { get; set; }
    public string OldParent { get; set; }
    public string OldChild { get; set; }

    // Migration metadata       
    public bool MigrationReady { get; set; }        
    public int LoadPriortity { get; set; }
    public int Attempt { get; set; }        
    public int Result { get; set; }
    public bool PropertiesUploaded { get; set; }
    [StringLength(14)]
    public string NewId { get; set; }

    [ForeignKey("CabinetConfiguration")]
    public string Cabinet { get; set; }
    public string Name { get; set; }
    public string Extension { get; set; }


    public bool InheritsSecurity { get; set; } 

    public virtual ICollection<DocumentVersion> DocumentVersions { get; set; }
    public virtual ICollection<DocumentLog> DocumentLogs { get; set; }

    [InverseProperty("DocumentParent")]
    public virtual ICollection<DocumentLinking> DocumentParents { get; set; }
    [InverseProperty("DocumentLink")]
    public virtual ICollection<DocumentLinking> DocumentLinks { get; set; }

    public virtual CabinetConfiguration CabinetConfiguration { get; set; }

    public Document()
    {
        DocumentVersions = new HashSet<DocumentVersion>();            
        DocumentLogs = new HashSet<DocumentLog>();            
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...