238

Here's the table

Users

UserId
UserName
Password
EmailAddress

and the code..

public void ChangePassword(int userId, string password){
//code to update the password..
}
Edward Brey
  • 40,302
  • 20
  • 199
  • 253
h3n
  • 5,142
  • 9
  • 46
  • 76

17 Answers17

437

Update: If you're using EF Core 7.0 or above, see this answer.

Ladislav's answer updated to use DbContext (introduced in EF 4.1):

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();
    }
}
Arad Alvand
  • 8,607
  • 10
  • 51
  • 71
Stuart
  • 5,106
  • 2
  • 23
  • 18
  • 62
    I was only able to make this code work by adding db.Configuration.ValidateOnSaveEnabled = false; before db.SaveChanges() ? – Jake Drew Jul 08 '13 at 06:19
  • 3
    Which namespace to include to use `db.Entry(user).Property(x => x.Password).IsModified = true;` and not `db.Entry(user).Property("Password").IsModified = true;` – Johan Aug 14 '13 at 19:43
  • 5
    This approach throws an OptimisticConcurencyException when the table has a timestamp field. – Maksim Vi. Apr 30 '14 at 20:33
  • @MaksimVi. add the timestamp field to the new User with the existing value. – Danny Varod Aug 04 '15 at 15:51
  • 9
    I think is worth mention that if you are using `db.Configuration.ValidateOnSaveEnabled = false;` you might want to keep validating the field you are updating: `if (db.Entry(user).Property(x => x.Password).GetValidationErrors().Count == 0)` – Ziul Aug 27 '15 at 23:28
  • If you don't want to set `db.Configuration.ValidateOnSaveEnabled = false` I was able to get this to work `db.Entry(user).State = EntityState.Unchanged; db.Entry(user).Property(x => x.Password).IsModified = true;` – dandax Oct 05 '16 at 19:21
  • It is also worth mentioning that this only works on primitive properties. Complex types (i.e. navigation property to some related model in a 1:N relationship) can't be updated this way, unfortunately. – Matt Borja Dec 12 '16 at 22:40
  • 3
    You need to set ValidateOnSaveEnabled to false if you have required fields in your table that you are not providing during your update – Sal Jan 19 '17 at 16:27
  • If you want to use this approach and keep the `ValidateOnSave`enabled, then see answers of this [question](http://stackoverflow.com/q/12871892/4625305). They show how to do it without too much hassle. – AXMIM May 12 '17 at 02:26
  • 1
    Also worth mention that this is slow like a turtle when updating many entities. It took more than 1 minute to update 2500 entities. I guess it's fine when you have only very few entities to update. The slowness probably lie around `.Attach()`. – AXMIM May 15 '17 at 22:44
  • 2
    @dandax Setting `db.Entry(user).State = EntityState.Unchanged` and then setting IsModified for the property I want updated did not work for me, still getting the validation error on another property. – Søren Boisen Nov 13 '17 at 20:50
  • @JakeDrew To add to Sal's comment, check out this question https://stackoverflow.com/questions/7795300/validation-failed-for-one-or-more-entities-see-entityvalidationerrors-propert Turning off validation entirely is a bad idea. – null Dec 06 '17 at 16:59
  • This also doesn't work if the object is already tracked, it will throw an InvalidOperationException upon Attach() – Michael Brown Jun 30 '18 at 00:51
  • Excellent! I have been struggling sending an update to the database because I had an object already tracked by a context. Decided to create a new object and update just one field and this post was the exact thing I needed. Thanks. – Edgar J. Rodriguez Oct 21 '18 at 00:34
  • @stuart Assuming the app implements a repository pattern, where might you suggest placing the above method? In the repo, or other? It feels funny to put in the repo because it isn't using repo pattern methods, but not sure. – Matthew Jan 24 '20 at 22:20
  • @Ziul re the comment on GetValidationErrors().Count == 0 for each property, if you check the left-hand side rowsAffected = dbCtx.SaveChanges() > 0 it should be sufficient; Also place a try/catch around your EF Code, and also enforce some constraints at the DB itself. – joedotnot Mar 07 '20 at 19:30
  • we don't have to load the user object but it would be better to check the user object with `userId` whether it exists with `Any()` method. – ninbit Jun 10 '20 at 16:15
  • 1
    @JakeDrew Im not able to use this: db.Configuration.ValidateOnSaveEnabled = false; It says, db does not contain a definition for 'Configuration' –  Jul 21 '21 at 12:25
  • do we need to set ` db.Configuration.ValidateOnSaveEnabled = false;` every time or only once? like i wanted to know in same request on next save this setting will be still there? – sairfan Jun 09 '23 at 21:22
  • An additional issue with this approach is when your entity has navigation properties then those without key values will be marked as Added. – Damian Ubowski Aug 13 '23 at 12:44
54

You can tell which properties have to be updated in this way:

public void ChangePassword(int userId, string password)
{
  var user = new User { Id = userId, Password = password };
  using (var context = new ObjectContext(ConnectionString))
  {
    var users = context.CreateObjectSet<User>();
    users.Attach(user);
    context.ObjectStateManager.GetObjectStateEntry(user)
           .SetModifiedProperty("Password");
    context.SaveChanges();
  }
}
surfmuggle
  • 5,527
  • 7
  • 48
  • 77
Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
30

In Entity Framework Core, Attach returns the entry, so all you need is:

var user = new User { Id = userId, Password = password };
db.Users.Attach(user).Property(x => x.Password).IsModified = true;
db.SaveChanges();
Edward Brey
  • 40,302
  • 20
  • 199
  • 253
29

New EF Core 7 native feature — ExecuteUpdate:

Finally! After a long wait, EF Core 7.0 now has a natively supported way to run UPDATE (and also DELETE) statements while also allowing you to use arbitrary LINQ queries (.Where(u => ...)), without having to first retrieve the relevant entities from the database: The new built-in method called ExecuteUpdate — see "What's new in EF Core 7.0?".

ExecuteUpdate is precisely meant for these kinds of scenarios, it can operate on any IQueryable instance, and lets you update specific columns on any number of rows, while always issuing a single UPDATE statement behind the scenes, making it as efficient as possible.

Usage:

Let's take OP's example — i.e. updating the password column of a specific user:

dbContext.Users
    .Where(u => u.Id == someId)
    .ExecuteUpdate(b =>
        b.SetProperty(u => u.Password, "NewPassword")
    );

As you can see, calling ExecuteUpdate requires you to make calls to the SetProperty method, to specify which property to update, and also what new value to assign to it.

EF Core will translate this into the following UPDATE statement:

UPDATE [u]
    SET [u].[Password] = "NewPassword"
FROM [Users] AS [u]
WHERE [u].[Id] = someId

Also, ExecuteDelete for deleting rows:

There's also a counterpart to ExecuteUpdate called ExecuteDelete, which, as the name implies, can be used to delete a single or multiple rows at once without having to first fetch them.

Usage:

// Delete users that haven't been active in 2022:
dbContext.Users
    .Where(u => u.LastActiveAt.Year < 2022)
    .ExecuteDelete();

Similar to ExecuteUpdate, ExecuteDelete will generate DELETE SQL statements behind the scenes — in this case, the following one:

DELETE FROM [u]
FROM [Users] AS [u]
WHERE DATEPART(year, [u].[LastActiveAt]) < 2022

Other notes:

  • Keep in mind that both ExecuteUpdate and ExecuteDelete are "terminating", meaning that the update/delete operation will take place as soon as you call the method. You're not supposed to call dbContext.SaveChanges() afterwards.
  • If you're curious about the SetProperty method, and you're confused as to why ExectueUpdate doesn't instead receive a member initialization expression (e.g. .ExecuteUpdate(new User { Email = "..." }), then refer to this comment (and the surrounding ones) on the GitHub issue for this feature.
  • Furthermore, if you're curious about the rationale behind the naming, and why the prefix Execute was picked (there were also other candidates), refer to this comment, and the preceding (rather long) conversation.
  • Both methods also have async equivalents, named ExecuteUpdateAsync, and ExecuteDeleteAsync respectively.
Majid Shahabfar
  • 4,010
  • 2
  • 28
  • 36
Arad Alvand
  • 8,607
  • 10
  • 51
  • 71
  • This does not work on table-per-type mapping strategy unfortunately `'ExecuteDelete'/'ExecuteUpdate' operations on hierarchies mapped as TPT is not supported. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.` – pkatsourakis Mar 16 '23 at 20:21
23

You have basically two options:

  • go the EF way all the way, in that case, you would
    • load the object based on the userId provided - the entire object gets loaded
    • update the password field
    • save the object back using the context's .SaveChanges() method

In this case, it's up to EF how to handle this in detail. I just tested this, and in the case I only change a single field of an object, what EF creates is pretty much what you'd create manually, too - something like:

`UPDATE dbo.Users SET Password = @Password WHERE UserId = @UserId`

So EF is smart enough to figure out what columns have indeed changed, and it will create a T-SQL statement to handle just those updates that are in fact necessary.

  • you define a stored procedure that does exactly what you need, in T-SQL code (just update the Password column for the given UserId and nothing else - basically executes UPDATE dbo.Users SET Password = @Password WHERE UserId = @UserId) and you create a function import for that stored procedure in your EF model and you call this function instead of doing the steps outlined above
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
14

i'm using this:

entity:

public class Thing 
{
    [Key]
    public int Id { get; set; }
    public string Info { get; set; }
    public string OtherStuff { get; set; }
}

dbcontext:

public class MyDataContext : DbContext
{
    public DbSet<Thing > Things { get; set; }
}

accessor code:

MyDataContext ctx = new MyDataContext();

// FIRST create a blank object
Thing thing = ctx.Things.Create();

// SECOND set the ID
thing.Id = id;

// THIRD attach the thing (id is not marked as modified)
db.Things.Attach(thing); 

// FOURTH set the fields you want updated.
thing.OtherStuff = "only want this field updated.";

// FIFTH save that thing
db.SaveChanges();
groggyjava
  • 421
  • 4
  • 9
  • 1
    I get entity validation errors when I try this, but it sure looks cool. – devlord Sep 19 '13 at 16:17
  • Does not work this method!!!: maybe you need to give more details how to use it!!! - this is the error: "Attaching an entity of type 'Domain.Job' failed because another entity of the same type already has the same primary key value. This can happen when using the 'Attach' method or setting the state of an entity to 'Unchanged' or 'Modified' if any entities in the graph have conflicting key values. This may be because some entities are new and have not yet received database-generated key values." – Lucian Bumb Jan 19 '16 at 15:54
  • Perfec! Check my answer to see flexible approach for any model! – 697 Dec 10 '19 at 13:55
13

While searching for a solution to this problem, I found a variation on GONeale's answer through Patrick Desjardins' blog:

public int Update(T entity, Expression<Func<T, object>>[] properties)
{
  DatabaseContext.Entry(entity).State = EntityState.Unchanged;
  foreach (var property in properties)
  {
    var propertyName = ExpressionHelper.GetExpressionText(property);
    DatabaseContext.Entry(entity).Property(propertyName).IsModified = true;
  }
  return DatabaseContext.SaveChangesWithoutValidation();
}

"As you can see, it takes as its second parameter an expression of a function. This will let use this method by specifying in a Lambda expression which property to update."

...Update(Model, d=>d.Name);
//or
...Update(Model, d=>d.Name, d=>d.SecondProperty, d=>d.AndSoOn);

( A somewhat similar solution is also given here: https://stackoverflow.com/a/5749469/2115384 )

The method I am currently using in my own code, extended to handle also (Linq) Expressions of type ExpressionType.Convert. This was necessary in my case, for example with Guid and other object properties. Those were 'wrapped' in a Convert() and therefore not handled by System.Web.Mvc.ExpressionHelper.GetExpressionText.

public int Update(T entity, Expression<Func<T, object>>[] properties)
{
    DbEntityEntry<T> entry = dataContext.Entry(entity);
    entry.State = EntityState.Unchanged;
    foreach (var property in properties)
    {
        string propertyName = "";
        Expression bodyExpression = property.Body;
        if (bodyExpression.NodeType == ExpressionType.Convert && bodyExpression is UnaryExpression)
        {
            Expression operand = ((UnaryExpression)property.Body).Operand;
            propertyName = ((MemberExpression)operand).Member.Name;
        }
        else
        {
            propertyName = System.Web.Mvc.ExpressionHelper.GetExpressionText(property);
        }
        entry.Property(propertyName).IsModified = true;
    }

    dataContext.Configuration.ValidateOnSaveEnabled = false;
    return dataContext.SaveChanges();
}
Community
  • 1
  • 1
Doku-so
  • 346
  • 3
  • 9
  • 1
    When I use this it gives me following error, Cannot convert Lambda expression to Type 'Expression> [] ' because it is not a delegate type – Imran Rizvi May 16 '13 at 08:05
  • @ImranRizvi, you simply need to update the parameters to: public int Update(T entity, params Expression>[] properties) NOTE the params keyword before expression – dalcam Dec 02 '17 at 00:37
8

In EntityFramework Core 2.x there is no need for Attach:

 // get a tracked entity
 var entity = context.User.Find(userId);
 entity.someProp = someValue;
 // other property changes might come here
 context.SaveChanges();

Tried this in SQL Server and profiling it:

exec sp_executesql N'SET NOCOUNT ON;
UPDATE [User] SET [someProp] = @p0
WHERE [UserId] = @p1;
SELECT @@ROWCOUNT;

',N'@p1 int,@p0 bit',@p1=1223424,@p0=1

Find ensures that already loaded entities do not trigger a SELECT and also automatically attaches the entity if needed (from the docs):

Finds an entity with the given primary key values. If an entity with the given primary key values is being tracked by the context, then it is returned immediately without making a request to the database. Otherwise, a query is made to the database for an entity with the given primary key values and this entity, if found, is attached to the context and returned. If no entity is found, then null is returned.

Randy Hall
  • 7,716
  • 16
  • 73
  • 151
Alexei - check Codidact
  • 22,016
  • 16
  • 145
  • 164
  • 1
    Doing a .Find() still returns the entity with all its data fields from what I can tell. So if you're looking to not query the entire entity and only update 1 field, this is not the solution. – René Sackers May 21 '21 at 15:23
7

I'm late to the game here, but this is how I am doing it, I spent a while hunting for a solution I was satisified with; this produces an UPDATE statement ONLY for the fields that are changed, as you explicitly define what they are through a "white list" concept which is more secure to prevent web form injection anyway.

An excerpt from my ISession data repository:

public bool Update<T>(T item, params string[] changedPropertyNames) where T 
  : class, new()
{
    _context.Set<T>().Attach(item);
    foreach (var propertyName in changedPropertyNames)
    {
        // If we can't find the property, this line wil throw an exception, 
        //which is good as we want to know about it
        _context.Entry(item).Property(propertyName).IsModified = true;
    }
    return true;
}

This could be wrapped in a try..catch if you so wished, but I personally like my caller to know about the exceptions in this scenario.

It would be called in something like this fashion (for me, this was via an ASP.NET Web API):

if (!session.Update(franchiseViewModel.Franchise, new[]
    {
      "Name",
      "StartDate"
  }))
  throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound));
GONeale
  • 26,302
  • 21
  • 106
  • 149
  • 2
    So your better solution is what Elisa? You should explicitly state what properties you allow to be updated (just like the white list required for ASP.NET MVC's `UpdateModel` command), that way you ensure hacker form injection cannot occur and they cannot update fields they aren't allowed to update. If however somebody can convert the string array to some sort of lambda expressions parameter and work with it in the `Update`, great – GONeale Jan 24 '13 at 00:33
  • 3
    @GONeale - Just passing through. Someone's [cracked it using Lambdas!](http://stackoverflow.com/a/5749469/609176) – David Spence Sep 18 '13 at 21:28
  • 1
    @Elisa It can be improved by using Func> instead of string[] – Spongebob Comrade Nov 06 '13 at 09:58
  • Even later to the game, and perhaps this is much more recent syntax, but `var entity=_context.Set().Attach(item);` followed by `entity.Property(propertyName).IsModified = true;` in the loop should work. – Auspex Mar 07 '18 at 16:03
6

Entity framework tracks your changes on objects that you queried from database via DbContext. For example if you DbContext instance name is dbContext

public void ChangePassword(int userId, string password){
     var user = dbContext.Users.FirstOrDefault(u=>u.UserId == userId);
     user.password = password;
     dbContext.SaveChanges();
}
Bahtiyar Özdere
  • 1,818
  • 17
  • 21
3

I know this is an old thread but I was also looking for a similar solution and decided to go with the solution @Doku-so provided. I'm commenting to answer the question asked by @Imran Rizvi , I followed @Doku-so link that shows a similar implementation. @Imran Rizvi's question was that he was getting an error using the provided solution 'Cannot convert Lambda expression to Type 'Expression> [] ' because it is not a delegate type'. I wanted to offer a small modification I made to @Doku-so's solution that fixes this error in case anyone else comes across this post and decides to use @Doku-so's solution.

The issue is the second argument in the Update method,

public int Update(T entity, Expression<Func<T, object>>[] properties). 

To call this method using the syntax provided...

Update(Model, d=>d.Name, d=>d.SecondProperty, d=>d.AndSoOn); 

You must add the 'params' keyword in front of the second arugment as so.

public int Update(T entity, params Expression<Func<T, object>>[] properties)

or if you don't want to change the method signature then to call the Update method you need to add the 'new' keyword, specify the size of the array, then finally use the collection object initializer syntax for each property to update as seen below.

Update(Model, new Expression<Func<T, object>>[3] { d=>d.Name }, { d=>d.SecondProperty }, { d=>d.AndSoOn });

In @Doku-so's example he is specifying an array of Expressions so you must pass the properties to update in an array, because of the array you must also specify the size of the array. To avoid this you could also change the expression argument to use IEnumerable instead of an array.

Here is my implementation of @Doku-so's solution.

public int Update<TEntity>(LcmsEntities dataContext, DbEntityEntry<TEntity> entityEntry, params Expression<Func<TEntity, object>>[] properties)
     where TEntity: class
    {
        entityEntry.State = System.Data.Entity.EntityState.Unchanged;

        properties.ToList()
            .ForEach((property) =>
            {
                var propertyName = string.Empty;
                var bodyExpression = property.Body;
                if (bodyExpression.NodeType == ExpressionType.Convert
                    && bodyExpression is UnaryExpression)
                {
                    Expression operand = ((UnaryExpression)property.Body).Operand;
                    propertyName = ((MemberExpression)operand).Member.Name;
                }
                else
                {
                    propertyName = System.Web.Mvc.ExpressionHelper.GetExpressionText(property);
                }

                entityEntry.Property(propertyName).IsModified = true;
            });

        dataContext.Configuration.ValidateOnSaveEnabled = false;

        return dataContext.SaveChanges();
    }

Usage:

this.Update<Contact>(context, context.Entry(modifiedContact), c => c.Active, c => c.ContactTypeId);

@Doku-so provided a cool approach using generic's, I used the concept to solve my issue but you just can't use @Doku-so's solution as is and in both this post and the linked post no one answered the usage error questions.

PWS
  • 101
  • 1
  • 1
  • 5
  • I was working on your solution when program passes line `entityEntry.State = EntityState.Unchanged;` all updated values in parameter `entityEntry` get revert, so no changes are saved, can you please help on it, thanks – sairfan Oct 24 '18 at 17:45
1

Combining several suggestions I propose the following:

    async Task<bool> UpdateDbEntryAsync<T>(T entity, params Expression<Func<T, object>>[] properties) where T : class
    {
        try
        {
            var entry = db.Entry(entity);
            db.Set<T>().Attach(entity);
            foreach (var property in properties)
                entry.Property(property).IsModified = true;
            await db.SaveChangesAsync();
            return true;
        }
        catch (Exception ex)
        {
            System.Diagnostics.Debug.WriteLine("UpdateDbEntryAsync exception: " + ex.Message);
            return false;
        } 
    }

called by

UpdateDbEntryAsync(dbc, d => d.Property1);//, d => d.Property2, d => d.Property3, etc. etc.);

Or by

await UpdateDbEntryAsync(dbc, d => d.Property1);

Or by

bool b = UpdateDbEntryAsync(dbc, d => d.Property1).Result;
Guy
  • 1,232
  • 10
  • 21
  • How to make this method available for other class, may be like extension method? – Velkumar Jun 06 '16 at 13:04
  • in [this .NET CORE tutorial](https://docs.asp.net/en/latest/data/ef-mvc/crud.html) they show best practice using (the new) EF Core to update specific properties in MVC. look for 'TryUpdateModelAsync'. – Guy Oct 12 '16 at 12:21
  • 1
    @Guy Awesome. Though, once more Microsoft's "best practice" is to do something other than what their tools build... – Auspex Mar 07 '18 at 16:42
1

I use ValueInjecter nuget to inject Binding Model into database Entity using following:

public async Task<IHttpActionResult> Add(CustomBindingModel model)
{
   var entity= await db.MyEntities.FindAsync(model.Id);
   if (entity== null) return NotFound();

   entity.InjectFrom<NoNullsInjection>(model);

   await db.SaveChangesAsync();
   return Ok();
}

Notice the usage of custom convention that doesn't update Properties if they're null from server.

ValueInjecter v3+

public class NoNullsInjection : LoopInjection
{
    protected override void SetValue(object source, object target, PropertyInfo sp, PropertyInfo tp)
    {
        if (sp.GetValue(source) == null) return;
        base.SetValue(source, target, sp, tp);
    }
}

Usage:

target.InjectFrom<NoNullsInjection>(source);

Value Injecter V2

Lookup this answer

Caveat

You won't know whether the property is intentionally cleared to null OR it just didn't have any value it. In other words, the property value can only be replaced with another value but not cleared.

Community
  • 1
  • 1
Korayem
  • 12,108
  • 5
  • 69
  • 56
1
_context.Users.UpdateProperty(p => p.Id, request.UserId, new UpdateWrapper<User>()
                {
                    Expression = p => p.FcmId,Value = request.FcmId
                });
   await _context.SaveChangesAsync(cancellationToken);

Update Property is an extension method

public static void UpdateProperty<T, T2>(this DbSet<T> set, Expression<Func<T, T2>> idExpression,
            T2 idValue,
            params UpdateWrapper<T>[] updateValues)
            where T : class, new()
        {
            var entity = new T();
            var attach = set.Attach(entity);
            attach.Property(idExpression).IsModified = false;
            attach.Property(idExpression).OriginalValue = idValue;
            foreach (var update in updateValues)
            {
                attach.Property(update.Expression).IsModified = true;
                attach.Property(update.Expression).CurrentValue = update.Value;
            }
        }

And Update Wrapper is a class

public class UpdateWrapper<T>
    {
        public Expression<Func<T, object>> Expression  { get; set; }
        public object Value { get; set; }
    }
supernerd
  • 379
  • 4
  • 13
  • To me this is in the category "how to complicate a simple task". For one, why attach a new object while the original is already tracked? You'll even get an exception doing this. And again, this fails with optimistic concurrency and possibly with validation. – Gert Arnold Nov 19 '20 at 09:47
  • The original is not tracked or even fetched from the db. Validation is not handled in this area of code as that was not part of the question – supernerd Nov 19 '20 at 10:47
  • They are tracked in your example. When creating stub entities, validation and optimistic concurrency should always be mentioned as caveats. Anyway, this is yet another answer that says "set `IsModified = true`". Does it really add anything new to show yet another method of doing that? That's isn't asked either. Old questions collecting more and more noise is quite an annoying problem on Stack Overflow. – Gert Arnold Nov 19 '20 at 11:06
  • @GertArnold true I am using IsModified true, which is already mentioned in other answers. What new I am offering is to do it via Extension methods and making it more generic such that we can specify Id also of the entity which either can be any data type and also which properties to update without fetching them from db. But if you don't find it helpful its up to you, someone would want it to implement via extension methods or any other way, I am just contributing to help others. – supernerd Nov 19 '20 at 11:25
0

I was looking for same and finally I found the solution

using (CString conn = new CString())
{
    USER user = conn.USERs.Find(CMN.CurrentUser.ID);
    user.PASSWORD = txtPass.Text;
    conn.SaveChanges();
}

believe me it work for me like a charm.

-2
public async Task<bool> UpdateDbEntryAsync(TEntity entity, params Expression<Func<TEntity, object>>[] properties)
{
    try
    {
        this.Context.Set<TEntity>().Attach(entity);
        EntityEntry<TEntity> entry = this.Context.Entry(entity);
        entry.State = EntityState.Modified;
        foreach (var property in properties)
            entry.Property(property).IsModified = true;
        await this.Context.SaveChangesAsync();
        return true;
    }
    catch (Exception ex)
    {
        throw ex;
    }
}
Thomas Fritsch
  • 9,639
  • 33
  • 37
  • 49
-8
public void ChangePassword(int userId, string password)
{
  var user = new User{ Id = userId, Password = password };
  using (var db = new DbContextName())
  {
    db.Entry(user).State = EntityState.Added;
    db.SaveChanges();
  }
}
Rahul
  • 2,309
  • 6
  • 33
  • 60