This exception comes when you are trying to associate a record (account) to another record (account) having N:N relationship using C#. In my case account can have multiple payer accounts, got this exception when I was trying to associate payer account.
If you get this exception you need to mention relationship PrimaryEntityRole is EntityRole.Referenced or EntityRole.Referencing. Here is sample code
public static void CreateAccountPayerRelationship(IOrganizationService service, Guid accountId, Guid payerAccountId)
{
Relationship relation = new Relationship("ls_account_account_payer");
relation.PrimaryEntityRole = EntityRole.Referenced;
EntityReference erPayerAccount = new EntityReference("account", payerAccountId);
service.Associate("account", accountId, relation, new EntityReferenceCollection() { erPayerAccount });
}