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的建议做了,那么你应该接受他们的答案。