0
\$\begingroup\$

I have a table that name is Store , In Store Table , just one row can IsDefault=true at time . when I insert new Row , I check If user selected IsDefault , I update other row whice isDefault=true . I use this code :

public AddStatus Add(AddStoreViewModel storeViewModel)
    {
        if (Exists(storeViewModel.Name)) return AddStatus.Exists;
        var storeModel = Mapper.Map(storeViewModel, new StoreEntity.Store());
        if (storeModel.IsDefault)
        {
            var defaultStore = GetDefault();
            if (defaultStore != null)
            {
                defaultStore.IsDefault = false;
                _uow.MarkAsBaseChanged(defaultStore); // update
            }
        }
        _uow.MarkAsBaseAdded(storeModel);
        return AddStatus.Successfull;
    }

and in controller I call Above Method like belowe and Call SaveAllChanges :

 _storeService.Add(storeViewModel);
 await _uow.SaveChangesAsync();

and MarkAsBaseChange like belowe :

  public void MarkAsBaseChanged<TEntity>(TEntity entity) where TEntity : BaseEntity
    {
        Entry(entity).Entity.Action = Enums.AuditAction.Update;
}

is this code ok ?

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Does _uow.MarkAsBaseChanged() primarily hide EF's SaveChanges()? If so, make a new version that can take an array of objeects.

You would want both objects saved with one command to ensure the transaction. As it ia nw, if storeModel fails you no longer have a default store.

\$\endgroup\$
3
  • \$\begingroup\$ pleas see new update \$\endgroup\$ Commented May 3, 2016 at 5:56
  • \$\begingroup\$ @Rahimi Then my answer stands. You have a hole in your logic if second save fails. \$\endgroup\$ Commented May 3, 2016 at 5:58
  • \$\begingroup\$ I dont call SaveChanges in MarkAsBaseChanged . In MarkAsBaseChanged I just Save some Log like EditorIP ,... \$\endgroup\$ Commented May 3, 2016 at 6:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.