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 ?