/// <summary> /// Helper function that was used when DB indexer fails. /// This function is not used in production. /// </summary> /// <param name="item">Class type</param> /// <returns>Class type object</returns> public T Identify(T item) { try { Type type = item.GetType(); object obj = Activator.CreateInstance(type); obj = item; PropertyInfo prop = type.GetProperty("ID"); Type propertyType = prop.PropertyType; TypeCode typeCode = Type.GetTypeCode(propertyType); int oldID = (int)prop.GetValue(obj, null); if (prop != null && oldID <= 0) { int value = _ctx.Set <T>().Count() + 1; prop.SetValue(obj, value, null); } return((T)obj); } catch (Exception e) { Console.WriteLine(e); } return(null); }
/// <summary> /// Method adds item of type T to the dbSet<T>. /// </summary> /// <param name="item">Item of type T</param> public void Add(T item) { try { _ctx.Set <T>().Add(item); } catch (Exception e) { _logger.LogInformation($"Problem with add {typeof(T).FullName} : {e}"); } }