This was not a law that was “passed” it was a law that had always existed. To say that God Himself had changed His own law between Genesis and Deuteronomy, would put the burden of proof on you, not me. When following the text, it is reasonable to say that the law had always existed and was passed down to the people, and explained to the people by Moses, starting in chapter 4. By saying that the law was passed, you are either saying that it was passed like a law today, by humans, or that God changed His mind. Either way, the burden of proof remains with you.
There are two events that happen here, that offend the Lord. First, Onan disobeying the law, as presented by Moses in Dt. 25:5-10, and Onan spilling his seed on the ground. Both are despicable in the eyes of the Lord, though one of them only carries with it the penalty of shame (Dt. 25: 5-10) while the other, according to this passage, carries with it the penalty of death. As I said above, if you are to say that the law went into effect with Moses in Deuteronomy, and did not exist before, you are saying that God’s people were lawless and therefore could not possibly disobey the Lord. And yet Onan, and his brother Er were both slain by the Lord for having offended Him.
Again, my friend, the burden of proof is on you. I’ve already quoted you Church Fathers, who lived much closer in time to that society than you and I do. Based on their analysis, which I showed you several posts ago, the process of procreation cannot be interrupted by man, as it was set in motion by God. While sexual relations can have other purposes (between man and wife only) they have to be open to life and cannot interfere when life happens by the Grace of God. Song of Songs gives us a vivid picture of pleasure but does not say anything about interrupting what God set into motion. If pleasure was not involved, many of us would not be here today.