Here are a couple examples showing both an ex post facto change and a non-ex post facto change.
Example 1: The building code is changed to say that all electrical outlets installed in kitchens must be installed with GFCI protection. Your non-GFCI kitchen outlets were installed before this change. In this case fining you for not having GFCI outlets probably would violate the ex post facto law prohibition because you are being fined for something that occurred before the new law--the installation of non-GFCI outlets.
Example 2: The building code is changed to prohibit non-GFCI outlets in kitchens. Your non-GFCI kitchen outlets were installed before this change. Fining you for having non-GFCI outlets would probably not violate the ex post facto law prohibition because you are being fined for something that is happening after the law--having non-GFCI outlets in your kitchen.
(This is all assuming that fines for building code violations are punitive. The prohibition against ex post facto laws in the US generally only applies to changes that increase punitive aspects of laws).
My personal experience having owned multiple properties is that code changes rarely, if ever work the way you are describing. I've actually renovated a few kitchens, and had to deal with GFCI because the law around the permit for construction required us to, not because the code change was applied to pre-existing non-GFCI outlets. It would be impossible to maintain a property if the law worked as you described - the government could declare the kind of shingles on your roof illegal, and take your property for non-compliance, which is exactly why ex post facto laws are unconstitutional.
In the US ex post-facto laws are generally unconstitutional. Our law defaults to grandfathering (there are exceptions), so a change in code applies to new construction, or renovations to existing structures. My experience owning older properties is that this was not a feature. Often what would be a repair in a new property would lead to a major renovation because whatever was being worked on would have to "be brought up to code".