Populating Neo4J with Custom C# application

Hi, I am new to Neo4J and have done some reading and research but not sure if what I need to do is possible or available.

I would like to populate a Neo4J database with data which does not come from a standard source like RDBMS or CSV. The ideal way to enter this data is a combination of a) manual entry of new data from a webpage or Windows application and b) insert existing data via the C# application which converts the data another format into suitable C# or API calls to Neo4J.

The above is preferred so users who are not technical (e.g. data entry clerks) can populate the database as teaching them Cypher or programming is not an option.

One obvious solution is to generate a text file containing many Cypher statements and then submitting it to Neo4j. But is a more direct and programmatic approach available? If so, can anyone point me to a sample project or discussion thread?

thanks!

Tom

Hi Tom

I am still very new to Neo4j myself, so take my advice with a grain of salt. I was in a similar situation and found this blog helpful in the beginning:

From the above all my Model classes contains a AsDictionary method. Then you can create a method to generate a CREATE query which takes the Dictionary as input. This means you don't have to write individual CREATE queries for all your Models.

private static string CreateNodeQuery(Dictionary<string, object> properties, string type)
{
	var query = new StringBuilder();

	query.AppendLine($"CREATE (node: {type} {{");

	foreach (KeyValuePair<string, object> property in properties)
	{
		if (property.Key != properties.Last().Key)
		{
			query.AppendLine($"{property.Key}: $node.{property.Key}, ");
		}
		else
		{
			query.AppendLine($"{property.Key}: $node.{property.Key} }} )");
		}
	}

	query.AppendLine($"RETURN node");

	return query.ToString();
}

Whether or not the above is a good solution, remains to be seem, but it seems to work in my case.

It gets more interesting when you have nested classes, as this, in my case, must be turned into a new node connected by a relationship. I solved this by using simple ViewModels to create nodes and relationships and then use the full model when retrieving nodes. For the last part I had a question answered here:

Hope it helps :slight_smile:

Best regards
Andreas

Hi Andreas,

Thanks so much for the detailed response.

Yes I am sure your method works well and I will use this if I cannot find a more direct method. I was wondering if there exists for example an OO set of classes to insert nodes and relationships and attributes rather than generating Cypher statements which executes against the db. Similar to Ado.net and Entity Framework.

For now this will be perfectly satisfactory as I am mostly experimenting with Neo4j.

Your dictionary use is a good idea.

thanks again!
Tom

Hi Tom

Sorry for my late response. I didn’t get a notification about your reply.

I haven’t found an official Neo4j Object Graph Mapping (OGM) for .NET (nor one with a large userbase). There is one for Java:

So it is definitely possible, so maybe something like this will exist at some point for .NET (or maybe it already exist and I just haven’t found it yet).

Best regards
Andreas

Hi Andreas,

Thanks, I looked around and found nothing for .Net which is a little surprising. There are commercial OGM tools which works with Neo4J amongst others. I need to educate myself first and see what the best option would be.

I would be happy to learn Java except our codebase is in C#.

If I discover anything useful I will let you know.

Tacks sa myket!

Tom