title: update entity in DDD with EFCore 3
body: <p>i have senario for change Active Status property use . <code>IsActive</code> .</p>
  
  <p>i have problem with update entity in asp core and DomainDriven Desogn .</p>
  
  <p>when i change the <code>IsActive</code> to <code>True</code> or <code>False</code> it create a new record inctance a update that recoed . </p>
  
  <p>i put here my code :</p>
  
  <p><strong>Step One :</strong></p>
  
  <p>this is my controller , in this controller i call the Command :</p>
  
  <pre><code>        public async Task&lt;ApiReturn&gt; ChangeUserActiveStatus(long id)
      {
          var result = await dispatchers.SendAsync(new UserActiveStateCommand { id = id });
          if(result.Success)
          {
              return Ok();
          }
          return BadRequest(result.Exception);
      }
  </code></pre>
  
  <p><strong>step Two :</strong>
  i send a request to the database and give The desired record :</p>
  
  <pre><code>    public class UserActiveStateCommandHandler : ICommandHandler&lt;UserActiveStateCommand, OperationResult&lt;string&gt;&gt;, IScopedDepency
  {
      private readonly IDomainUnitOfWork unitOfWork;
      private readonly IDispatchers dispatchers;
  
      public UserActiveStateCommandHandler(IDomainUnitOfWork unitOfWork, IDispatchers dispatchers)
      {
          this.unitOfWork = unitOfWork;
          this.dispatchers = dispatchers;
      }
      public async Task&lt;OperationResult&lt;string&gt;&gt; HandlerAsync(UserActiveStateCommand command)
      {
          var user = await dispatchers.QueryAsync(new GetUserByIdQuery { id = command.id });
          if (user != null)
          {
              var u = new User();
              var value = u.SetUser(user.Result.Id, user.Result.Email, user.Result.LastName, user.Result.Photo, user.Result.FirstName, user.Result.UserName, user.Result.PhoneNumber, !user.Result.IsActive);
              await dispatchers.CallEvent(new UserActiveStateEvent { User = value });
              return OperationResult&lt;string&gt;.BuildSuccessResult("success");
          }
          else
          {
              return OperationResult&lt;string&gt;.BuildFailure("fail");
          }
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>this is that recoed and its correct :</p>
  
  <pre><code>    "email": "kiadr9372@gmail.com",
      "photo": null,
      "lastName": "Dortaj",
      "firstName": "Kianoush",
      "userName": "kia9372",
      "phoneNumber": "09159810616",
      "securityStamp": "e7cd82a5-71f3-41a1-b239-4d705b6d5d35",
      "isActive": false,
      "phoneConfirm": false,
      "emailConfirm": false,
      "isLockOut": false,
      "lockOutEnd": null,
      "isFailurAccount": 0,
      "userRoles": null,
      "id": 1
  </code></pre>
  
  <p><strong>step there :</strong></p>
  
  <p>i call Event for Update Record in database :</p>
  
  <pre><code> public class UserActiveStatusEventHandler : IEventHandler&lt;UserActiveStateEvent&gt; , IScopedDepency
  {
      private readonly IDomainUnitOfWork unitOfWork;
  
      public UserActiveStatusEventHandler(IDomainUnitOfWork unitOfWork)
      {
          this.unitOfWork = unitOfWork;
      }
      public async Task HandelEventAsync(UserActiveStateEvent @event)
      {
          var state = await unitOfWork.UserRepository.UpdateUser(@event.User);
          if (state.Success)
          {
              unitOfWork.Commit();
          }
      }
  }
  </code></pre>
  
  <p>this is the update code in repository :</p>
  
  <pre><code> public async Task&lt;OperationResult&lt;bool&gt;&gt; UpdateUser(User user)
      {
          if (user != null)
          {
              try
              {
                  context.Update(user);
                  return OperationResult&lt;bool&gt;.BuildSuccessResult(true);
              }
              catch (Exception ex)
              {
                  return OperationResult&lt;bool&gt;.BuildFailure(ex);
              }
          }
          return OperationResult&lt;bool&gt;.BuildFailure("user Cannot be null");
      }
  </code></pre>
  
  <p>and this is the commit code : </p>
  
  <pre><code>public void Commit()
      {
          context.SaveChanges();
      }
  </code></pre>
  
  <p>this is <code>User</code> Aggregate :</p>
  
  <pre><code>    public class User : AggregrateRoot, IAggregate
  {
      #region Backing Field
      private string _email;
      private string _photo;
      private string _userName;
      private string _phoneNumber;
      private string _firstName;
      private string _lastName;
      private Guid _securityStamp;
      private bool _isActive;
      private bool _phoneConfirm;
      private bool _emailConfirm;
      private bool _isLockOut;
      private DateTimeOffset? _lockOutEnd;
      private int _isFailurAccount;
      #endregion
  
      #region Properies
      public string Email
      {
          get
          {
              return _email;
          }
          private set
          {
              _email = value;
              SetNotification();
          }
      }
      public string Photo
      {
          get
          {
              return _photo;
          }
          set
          {
              _photo = value;
              SetNotification();
          }
      }
      public string LastName
      {
          get
          {
              return _lastName;
          }
          private set
          {
              _lastName = value;
              SetNotification();
          }
      }
      public string FirstName
      {
          get
          {
              return _firstName;
          }
          private set
          {
              _firstName = value;
              SetNotification();
          }
      }
      public string UserName
      {
          get
          {
              return _userName;
          }
          private set
          {
              _userName = value;
              SetNotification();
          }
      }
      public string PhoneNumber
      {
          get
          {
              return _phoneNumber;
          }
          private set
          {
              _phoneNumber = value;
              SetNotification();
          }
      }
      public Guid SecurityStamp
      {
          get
          {
              return _securityStamp;
          }
          private set
          {
              _securityStamp = value;
              SetNotification();
          }
  
      }
      public bool IsActive
      {
          get
          {
              return _isActive;
          }
          private set
          {
              _isActive = value;
              SetNotification();
          }
      }
      public bool PhoneConfirm
      {
          get
          {
              return _phoneConfirm;
          }
          private set
          {
              _phoneConfirm = value;
              SetNotification();
          }
      }
      public bool EmailConfirm
      {
          get
          {
              return _emailConfirm;
          }
          private set
          {
              _emailConfirm = value;
              SetNotification();
          }
      }
      public bool IsLockOut
      {
          get
          {
              return _isLockOut;
          }
          private set
          {
              _isLockOut = value;
              SetNotification();
          }
      }
      public DateTimeOffset? LockOutEnd
      {
          get
          {
              return _lockOutEnd;
          }
          private set
          {
              _lockOutEnd = value;
              SetNotification();
          }
  
      }
      public int IsFailurAccount
      {
          get
          {
              return _isFailurAccount;
          }
          private set
          {
              _isFailurAccount = value;
              SetNotification();
          }
      }
  
      public ICollection&lt;UserRole&gt; UserRoles { get; set; }
      #endregion
  
      public User()
      {
  
      }
  
      #region Private Constructor
      private User(long id,string Email, string LastName, string photo, string FirstName, string UserName, string PhoneNumber, bool isActive)
      {
          this.Email = Email;
          this.Photo = photo;
          this.LastName = LastName;
          this.FirstName = FirstName;
          this.UserName = UserName;
          this.PhoneNumber = PhoneNumber;
          this.SecurityStamp = Guid.NewGuid();
          this.IsActive = isActive;
          this.PhoneConfirm = false;
          this.EmailConfirm = false;
          this.IsLockOut = false;
          this.LockOutEnd = null;
          this.IsFailurAccount = 0;
      }
      #endregion
  
      #region Methonds
      public User SetUser(string email, string lastName, string photo, string firstName, string userName, string phoneNumber, bool isActive = false)
      {
          var user = new User(email, lastName, firstName, photo, userName, phoneNumber, isActive);
  
          return user;
      }
  
  
      public UserRole AddUserRole(long userId, long roleId)
      {
          return new UserRole().AddUserRole(userId, roleId);
      }
      #endregion
  }
  </code></pre>
  
  <p>but problem is here : <strong>i create a new recoed in datebase but i need update that recoed . how can i solve this problem ???</strong></p>