How to update only one field using Entity Framework?

前端 未结 16 1855
后悔当初
后悔当初 2020-11-22 09:09

Here\'s the table

Users

UserId
UserName
Password
EmailAddress

and the code..



        
16条回答
  •  梦谈多话
    2020-11-22 09:31

    public async Task UpdateDbEntryAsync(TEntity entity, params Expression>[] properties)
    {
        try
        {
            this.Context.Set().Attach(entity);
            EntityEntry 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;
        }
    }
    

提交回复
热议问题