NET中使用 Neo4j 5.20.0 学习记录

使用属性创建节点

语句
//创建单个节点
CREATE (n:Person $props)
RETURN n
//创建多个节点
UNWIND $props AS map
CREATE (n)
SET n = map

具体参数请访问 https://neo4j.com/docs/cypher-manual/current/clauses/create/

NET
  using Neo4jClient;
   private readonly BoltGraphClient _client;

   public MedicalGraphService()
   {
        //连接数据库
       _client = new BoltGraphClient(new Uri("bolt://localhost:7687"), "user", "pwd");
       _client.ConnectAsync().Wait();
   }
   
  public async Task<string> CreateMedicalGraph()
 {
    List<Doctor> doctors = new List<Doctor> {
    new Doctor { Name = "史密斯博士", Specialty = "心脏病科" },
    new Doctor { Name = "约翰逊医生", Specialty = "神经学科" } };
    
    try
    {
  //创建单个节点
await _client.Cypher .Create("(n:doctor $doctors)") .WithParam("doctors", doctors[0]) .ExecuteWithoutResultsAsync(); //创建多个节点 await _client.Cypher   .Unwind(doctors, "doctors")   .Create("(n:Doctor)")   .Set("n = doctors")   .ExecuteWithoutResultsAsync(); } catch (Exception ex) { return "创建失败"; } return "创建成功"; }

 

posted @ 2024-06-26 23:12  Sumi-eCrow  阅读(25)  评论(0编辑  收藏  举报