Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# &引用;“无法修改属性ID”;尝试更新其他属性时出错_C#_Entity Framework - Fatal编程技术网

C# &引用;“无法修改属性ID”;尝试更新其他属性时出错

C# &引用;“无法修改属性ID”;尝试更新其他属性时出错,c#,entity-framework,C#,Entity Framework,以前有人问过这个问题,每次回答都是代码正在更新实体的Id属性。我看不到我的代码如何修改此属性 System.InvalidOperationException:属性“Id”是对象密钥信息的一部分,无法修改。 位于System.Data.Entity.Core.Objects.EntityEntry.DetectChangesProperty(Int32序数,布尔detectOnlyComplexProperties,布尔detectOnly) 上面的错误出现在我将我的对象trunt属性设置为Is

以前有人问过这个问题,每次回答都是代码正在更新实体的Id属性。我看不到我的代码如何修改此属性

System.InvalidOperationException:属性“Id”是对象密钥信息的一部分,无法修改。 位于System.Data.Entity.Core.Objects.EntityEntry.DetectChangesProperty(Int32序数,布尔detectOnlyComplexProperties,布尔detectOnly)

上面的错误出现在我将我的对象
trunt
属性设置为
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>();            
    }
}
公共类文档
{
公共int Id{get;set;}
//以前的系统元数据
公共字符串DocumentKey{get;set;}
公共字符串OldParent{get;set;}
公共字符串OldChild{get;set;}
//迁移元数据
public bool MigrationReady{get;set;}
公共int装入优先级{get;set;}
公共int尝试{get;set;}
公共int结果{get;set;}
公共布尔属性上载{get;set;}
[第14段]
公共字符串NewId{get;set;}
[外键(“橱柜配置”)]
公共字符串文件柜{get;set;}
公共字符串名称{get;set;}
公共字符串扩展名{get;set;}
公共布尔继承安全性{get;set;}
公共虚拟ICollection DocumentVersions{get;set;}
公共虚拟ICollection DocumentLogs{get;set;}
[InverseProperty(“DocumentParent”)]
公共虚拟ICollection DocumentParents{get;set;}
[反向属性(“文档链接”)]
公共虚拟ICollection文档链接{get;set;}
公共虚拟CabinetConfiguration CabinetConfiguration{get;set;}
公共文件()
{
DocumentVersions=新的HashSet();
DocumentLogs=新的HashSet();
}
}

saveDocumentCreationDatabase
-这是新文档吗?不是,这是现有记录。我有一个循环,它从数据库中获取批量文档,然后将它们发布到调用此函数的
TransformBlock
(使用TPL数据流)。这允许我同时处理记录。
saveDocumentCreationDatabase
-这是新文档吗?不,这是现有记录。我有一个循环,它从数据库中获取批量文档,然后将它们发布到调用此函数的
TransformBlock
(使用TPL数据流)。这允许我同时处理记录。