Wan*_*Dan 2 c# entity-framework-core asp.net-core
我有Article和ApplicationUser模型类:
public class ApplicationUser
{
...
}
public class Article
{
...
public ApplicationUser CreatedBy { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我试图通过这种方式将 CreatedBy 属性设置为 false:
base.Entry(entity).Property(x => x.CreatedBy).IsModified = false;
Run Code Online (Sandbox Code Playgroud)
但我收到此错误:
正在使用“Property”方法访问实体类型“ApplicationUser”上的“CreatedBy”属性,但在模型中将其定义为导航属性。使用“Reference”或“Collection”方法访问导航属性。
如果我理解正确,文章实体可能如下所示:
public class Article
{
public int Id { get; set; }
public string UserID { get; set; }
// ...
[ForeignKey("UserID")]
public ApplicationUser CreatedBy { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
正如错误信息所描述的,CreatedBy这里是一个导航属性。
因此,将您的代码更改为
Entry(entity).Reference(x => x.CreatedBy).IsModified = false; ,
它可能会按预期工作。
| 归档时间: |
|
| 查看次数: |
1441 次 |
| 最近记录: |