public Entity Add(int typeId, string requiredAttributeValue) { var newEntity = new Entity(); EntityType type; var typeFromDb = _context.Types.FirstOrDefault(x => x.Id == typeId); if (typeFromDb == null) { throw new InvalidOperationException("Не найден тип для добавления сущности"); } var typeName = typeFromDb.Name; try { typeName = typeName.ToLower(); typeFromDb = _context.Types.FirstOrDefault(t => t.Name == typeName) ?? throw new ArgumentException("No such type found in db"); newEntity.TypeId = typeFromDb.Id; newEntity.Display = requiredAttributeValue; } catch (Exception e) { Console.WriteLine(e); return(null); } try { var entityEntry = AddEntityToDatabase(newEntity); var newProperty = AddRequiredPropertyAndReturnItFromDb(typeName, requiredAttributeValue, typeFromDb.RequiredProperty, entityEntry); var handler = new SqlHandler(_context); if (!handler.CheckIfDisplayAttributeIsSet(typeName)) { handler.AddDisplayAttribute(typeName, newProperty.Value); } return(entityEntry.Entity); } catch (Exception e) { Console.WriteLine(e); return(null); } }