Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.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# 如何将导航属性的IsModified设置为false_C#_Asp.net Core_Entity Framework Core - Fatal编程技术网

C# 如何将导航属性的IsModified设置为false

C# 如何将导航属性的IsModified设置为false,c#,asp.net-core,entity-framework-core,C#,Asp.net Core,Entity Framework Core,我有文章和应用程序用户模型类: public class ApplicationUser { ... } public class Article { ... public ApplicationUser CreatedBy { get; set; } } 我已尝试通过以下方式将CreatedBy属性设置为false: base.Entry(entity).Property(x => x.CreatedBy).IsModified = false; 但我得

我有
文章
应用程序用户
模型类:

public class ApplicationUser
{
    ...

}

public class Article
{
    ...

    public ApplicationUser CreatedBy { get; set; }
}
我已尝试通过以下方式将CreatedBy属性设置为false:

base.Entry(entity).Property(x => x.CreatedBy).IsModified = false;
但我得到了这个错误:

正在使用“property”方法访问实体类型“ApplicationUser”上的属性“CreatedBy”,但该属性在模型中定义为导航属性。使用“引用”或“集合”方法访问导航属性


我改为使用“引用”方法而不是“属性”方法访问
CreatedBy

base.Entry(entity).Reference(x => x.CreatedBy).IsModified = false;

如果我理解正确,文章实体可能如下所示:

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

    public string UserID { get; set; }

    // ...

    [ForeignKey("UserID")]
    public ApplicationUser CreatedBy { get; set; }
}
如错误信息所述,
CreatedBy
是此处的导航属性

所以把你的代码改成

Entry(entity).Reference(x=>x.CreatedBy).IsModified=false,


它可能会按预期工作。

您不能只使用
base.Entry(entity).CreatedBy.IsModified=false?如果你完全按照@itminus的建议做了,那么你应该接受他们的答案。