1

I can't seem to get the following code working.

I would like to use the following code

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

Instead of

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

This is a code snippet from an answer which can be found here Link.

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();
  }
}

My current usings

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Data.Entity.Infrastructure;
Community
  • 1
  • 1
Johan
  • 8,068
  • 1
  • 33
  • 46

1 Answers1

1

I don't think it is a namespace problem, but rather which overloaded version of DbContext.Entry() you use; the generic one or the non-generic one.

Try this:

db.Entry<User>(user).Property(x => x.Password)
Karl Anderson
  • 34,606
  • 12
  • 65
  • 80
  • My exact code. `DbEntityEntry dbEntityEntry = context.Entry(entity); dbEntityEntry.Property("IsActive").IsModified = true;` – Johan Aug 14 '13 at 20:16
  • But yes, I used the generic one and it all went great! Thanks – Johan Aug 14 '13 at 20:17