UK’s Brexit is not a special case as Brussels would like to think. Italy, Spain, Portugal, and possibly even France are in line to exit the EU. The UK is just a first, extreme manifestation of the trend which tears the EU apart from within. The UK is first to break apart because it was less attached to the EU integration mechanisms than the other EU countries.
The key EU problem is that it has never been integrated. Instead of equal cooperation between the EU countries, it had a hierarchy, the dominance of some countries and the subordination of others. Economic entities have never had equal conditions either. Instead, protectionism has been the case covered under the openness of economies.
Shameless curtail of economic freedoms accompanied by shouting about freedom of sexual and other minorities could be constrained with some generally accepted rules. However, the EU rules are imposed by a few grand countries, because the EU is a system of quotas and privileges. This dangerous mix could only lead to the present situation. The migrants issue became just a detonator to express general discontent.