public async Task <IActionResult> Add([FromBody] TEntity entity) { if (_BlockedFromAdd.Any(eName => eName == typeof(TEntity).Name)) { return(BadRequest(new Error() { Message = "Oops! this action is not available for this entity !!" })); } // throw new Exception("something went wrong!!"); // if autoId has value [ok] then generate newId and set keyName of this entity // otherwise the entity keyField [PK] has value [from User Input] if ( !_BlockedFromAddGeneratedId.Any(name => name == typeof(TEntity).Name) && entity.GetValue(_keyName).ToString() == "0" ) { // get the comma separated column names _columnNames = entity.GetPrimitivePropsNames(); // get the comma separated column values // note: key column value replaced with the sql variable [@newId] // which will be fulfilled before executing the insert statement _columnValues = entity.GetPrimitivePropsValues(); // build SQL Command that: // (1) get the max key value from entity table // (2) If it is = null then set it = 0 // (3) increment it by one // (4) insert the entity with its values // (5) select the previously inserted entity _sqlAddCommand = $@" Declare @newId int; set @newId = (select max({_keyName}) from {_tableName}); if @newId is null set @newId = 0; set @newId = @newId + 1; insert into {_tableName} ({_columnNames}) values ({_columnValues}); select * from {_tableName} where {_keyName} = @newId; "; Console.WriteLine(_sqlAddCommand); // execute SQL Command and return its value entity = _service.Add <TEntity>(_sqlAddCommand); } else { // add entity and saveChanges await Task.Run(() => _service.Add(entity)); } // await _hubContext.Clients.Group(_tableName).SendAsync(_clientMethod, "Insert", entity); return(_GetEntityResult(entity)); }
// Add an entity to DB public async Task Add(TEntity entity) { _clientMethod = "Added"; try { // if autoId has value [ok] then generate newId and set keyName of this entity // otherwise the entity keyField [PK] has value [from User Input] if (entity.GetValue(_keyName).ToString() == "0") { // get the comma separated column names _columnNames = entity.GetPrimitivePropsNames(); // get the comma separated column values // note: key column value replaced with the sql variable [@newId] // which will be fulfilled before executing the insert statement _columnValues = entity.GetPrimitivePropsValues(); // build SQL Command that: // (1) get the max key value from entity table // (2) If it is = null then set it = 0 // (3) increment it by one // (4) insert the entity with its values // (5) select the previously inserted entity _sqlAddCommand = $@" Declare @newId int; set @newId = (select max({_keyName}) from {_tableName}); if @newId is null set @newId = 0; set @newId = @newId + 1; insert into {_tableName} ({_columnNames}) values ({_columnValues}); select * from {_tableName} where {_keyName} = @newId; "; Console.WriteLine(_sqlAddCommand); // execute SQL Command and return its value entity = _service.Add <TEntity>(_sqlAddCommand); } else { // add entity and saveChanges _service.Add <TEntity>(entity); } // if everything is ok, return the full user obj with all inserted values await Clients.All.SendAsync(_clientMethod, new Response <TEntity>() { Data = entity }); } catch (Exception ex) { await Clients.All.SendAsync(_clientMethod, new Response <Exception>() { Exception = ex }); } }