要包含哪个命名空间以使用DbEntityEntry.property强类型属性

本文关键字:DbEntityEntry property 强类型 属性 包含哪 命名空间 | 更新日期: 2023-09-27 18:27:46

我似乎无法让以下代码正常工作。

我想使用以下代码

db.Entry(user).Property(x => x.Password)

代替

db.Entry(user).Property("Password")

这是一个答案的代码片段,可以在这里找到链接。

public void ChangePassword(int userId, string password)
{
  var user = new User() { Id = userId, Password = password };
  using (var db = new MyEfContextName())
  {
    db.Users.Attach(user);
    db.Entry(user).Property(x => x.Password).IsModified = true;
    db.SaveChanges();
  }
}

我当前使用的

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Data.Entity.Infrastructure;

要包含哪个命名空间以使用DbEntityEntry.property强类型属性

我不认为这是命名空间问题,而是您使用的DbContext.Entry()的哪个重载版本;通用的或非通用的。

试试这个:

db.Entry<User>(user).Property(x => x.Password)