【发布时间】:2019-06-15 17:21:46
【问题描述】:
在更新数据库中已经存在的数据时,该表具有 CreatedBy、CreatedDate、ModifiedBy、ModifiedDate 等列。 CreatedBy 和 CreatedDate 在创建新行时插入,但在更新时,我不需要更新 CreatedBy 和 CreatedDate,我只需要修改 ModifiedBy 和 ModifiedDate。
如以下代码所示,它会将 CreatedBy 更新为 null 并将 CreatedDate 更新为 01/01/0001 之类的东西。
_Context.UserProfile.Update(userProfile);
我的模型是这样的,
public class MyModelUserProfile
{
public int Id { get; set; }
public string Email { get; set; }
public string Username { get; set; }
public string FirstName { get; set; }
public string CreatedBy { get; set; }
public string ModifiedBy { get; set; }
}
有什么方法可以避免更新 CreatedBy 和 CreatedDate 列?我可以通过其电子邮件 ID 获取该特定记录来做到这一点,但我不想为此再次访问数据库。
【问题讨论】:
-
如下代码所示——这并没有说明为什么这些属性/字段应该被更新。取决于如何修改
userProfile。顺便说一句,日期属性在哪里? -
使用链接中的答案,但指定
Ignore而不是Throw。
标签: c# .net-core entity-framework-core dbcontext