N:N Dataloader Console CRM Dynamics

Hi everyone, this post is about how to insert the relationships between two entities that have been related using the N:N relationship type.
The scenario is that you are migrating data from and old third party system or maybe a different CRM system to a new CRM Dynamics application and you have decided that you need an N:N relationship so in the future the user can associate records using the subgrid on one of the entities.

But the question here is:  How can I insert the existing data into the new system?

There are some tools out there but there is always people like me that want to have our own methods or apps so we can manipulate them, so that is why I have made this console app.
You can download the example solution in visual studio 2012 using this link

Solution in Visual Studio

There are two main parts on the code, the first one is


Here we are adding an object to the relatedEntities collection
The second important part of the code is


Here we are validating if the relationship already exists , if not we proceed to insert/associate the records on the relatedEntities collection.