使用以下代码,我创建了两种类型的节点。在Neo4J
public async Task CreateData()
{
AddNodesAsync<Companies>(myCompanies);
AddNodesAsync<Employess>(myEmployes);
}
public Task AddNodesAsync<T>(List<T> nodes)
{
return client
.Cypher
.Create("(n:" + typeof(T).Name + " {nodes})")
.WithParams(new {nodes})
.ExecuteWithoutResultsAsync();
}
每个员工都有公司ID。 现在,当员工公司的ID与公司的ID匹配时,我希望将所有这些联系起来。
使用以下代码,我可以将一个员工与一个公司
相关联await client.Cypher
.Match("(company:Company)", "(employee:Employee)")
.Where((employee: company) => employee.ComanyId == company.Id)
.Create("employee-[:BELONGS_TO]->company")
.ExecuteWithoutResultsAsync();
但我怎么能告诉它应该将这个id ref雇用的所有人都链接到他们的对应公司?
答案 0 :(得分:1)
我认为某处存在拼写错误,这是我使用的代码,它会按照您的说法生成链接。
将这些用作POCO:
public class Company {
public int Id { get; set; }
}
public class Employee {
public int Id { get; set;}
public int CompanyId { get; set;}
}
添加它们:
var companies = new List<Company> { new Company { Id = 1 }, new Company { Id = 2 } };
var employees = new List<Employee>
{
new Employee{ Id=1, CompanyId =1 },
new Employee{ Id=2, CompanyId =1 },
new Employee{ Id=3, CompanyId =1 },
new Employee{ Id=4, CompanyId =2 },
new Employee{ Id=5, CompanyId =2 },
};
await AddNodesAsync(companies);
await AddNodesAsync(employees);
下一位按预期生成图表,关系全部正确创建。
await client.Cypher
.Match("(company:Company)","(employee:Employee)")
.Where((Company company, Employee employee) => employee.CompanyId == company.Id)
.Create("(employee)-[:BELONGS_TO]->(company)")
.ExecuteWithoutResults();