如何获得与Neo4jClient的属性的关系

问题描述:

经过一番努力才得到它的权利,我设法保存与Neo4j数据库的属性与Neo4jClient的关系。当我想要读回这些关系时,问题就出现了。突然之间,像魅力一样工作的查询不会返回我的用户了吗?没有抛出异常,通话只是默默地返回空:(我了解可能的反序列化的问题,并且增加了参数的构造函数的关系类,但没有运气如何获得与Neo4jClient的属性的关系

public class UserHasHomeCity : Relationship<HasHomeCity>, IRelationshipAllowingSourceNode<UserEntity>, IRelationshipAllowingTargetNode<CityEntity> 
{ 
    public UserHasHomeCity() 
     : base(-1, null) 
    { 
    } 

    public UserHasHomeCity(NodeReference targetNode, HasHomeCity data) 
     : base(targetNode, data) 
    { 
    } 

    public const string TypeKey = "USER_HAS_HOME_CITY"; 
    public override string RelationshipTypeKey 
    { 
     get { return TypeKey; } 
    } 
} 

public class HasHomeCity 
{ 
    public string Date { get; set; } 

    public HasHomeCity() 
    { } 

    public HasHomeCity(string date) 
    { 
     this.Date = date; 
    } 
} 

这里是我的查询:

 var graphResults = graphClient.Cypher 
      .Match("(user:User)-[:USER_IS_IN_ROLE]-(role:Role)", 
      "(user:User)-[:USER_HAS_HOME_CITY]-(homeCity:City)-[:CITY_IS_IN_COUNTRY]-(homeCountry:Country)", 
      "(user:User)-[:USER_HAS_LIVING_CITY]-(livingCity:City)-[:CITY_IS_IN_COUNTRY]-(livingCountry:Country)") 
      .Where((UserEntity user) => user.Id == id) 
      .Return((user, role, homeCity, livingCity, homeCountry, livingCountry) => new 
      { 
       User = user.As<UserEntity>(), 
       Roles = role.CollectAs<RoleEntity>(), 
       HomeCity = homeCity.As<CityEntity>(), 
       LivingCity = livingCity.As<CityEntity>(), 
       HomeCountry = homeCountry.As<CountryEntity>(), 
       LivingCountry = livingCountry.As<CountryEntity>() 
      }).Results; 
+0

您似乎没有返回任何关系?我曾预料过类似于'(user:User) - [r:USER_IS_IN_ROLE] - >(role:Role)''''''' –

Neo4jClient在可能的情况下正在使用RelationshipNode类,所以,好消息是 - 您不需要将您的关系定义为: Relationship!实际上,取决于您想要采用它多远,根本不需要你的UserHasHomeCity课程!

关系属性与节点相同,因为它们只是POCO对象。

所以,要创建(我敢肯定你知道),我们做这样的事情:

var userData = new User {Id = "Id-1"}; 
var cityData = new City {Name = "Brighton"}; 
var countryData = new Country {Name = "UK"}; 
var userHasHomeData = new HasHomeCity {Date = "April 1980"}; 
var generalData = new CountryRelationshipData { Area = "South Coast" }; 

gc.Cypher 
    .Create("(country:Country {countryParams})") 
    .WithParam("countryParams", countryData) 
    .ExecuteWithoutResults(); 

gc.Cypher 
    .Match("(country:Country)") 
    .Where((Country country) => country.Name == "UK") 
    .CreateUnique("(city:City {cityParams})-[:CITY_IS_IN_COUNTRY {relParams}]->(country)") 
    .WithParam("cityParams", cityData) 
    .WithParam("relParams", generalData) 
    .ExecuteWithoutResults(); 

gc.Cypher 
    .Match("(city:City)") 
    .Where((City city) => city.Name == "Brighton") 
    .Create("(user:User {userParams})-[:USER_HAS_HOME_CITY {relParams}]->(city)") 
    .WithParam("userParams", userData) 
    .WithParam("relParams", userHasHomeData) 
    .ExecuteWithoutResults(); 

,这将给我们一个(User)-[:USER_HAS_HOME_CITY]-(City)结构。

要检索的关系属性,我们可以用这个查询:

var query = gc.Cypher 
    .Match("(user:User)-[r:USER_HAS_HOME_CITY]-(city:City)-[r1:CITY_IS_IN_COUNTRY]-(country:Country)") 
    .Where((User user) => user.Id == "Id-1") 
    .Return((user, city, r, country, r1) => 
     new 
     { 
      User = user.As<User>(), 
      City = city.As<City>(), 
      HasHome = r.As<HasHomeCity>(), 
      Country = country.As<Country>(), 
      CountryRel = r1.As<CountryRelationshipData>() 
     }); 

和循环通过的结果(在这种情况下,所有的人都1):

var res = query.Results.ToList(); 
foreach (var result in res) 
    Console.WriteLine("User ({0}) home city: {1} (which is in {2}, {3}) since {4}", result.User.Id, result.City.Name,result.CountryRel.Area, result.Country.Name, result.HasHome.Date); 

会给我们:

User (Id-1) home city: Brighton (which is in South Coast, UK) since April 1980 

作为输出。

+0

感谢您指出我正确的方向,开始使用Neo4jClient最困难的部分肯定是缺乏像这样的最新示例。我将我的关系改为简单的poco类,结果证明我的问题是匹配语句的国家部分。你有没有看到明显的错误?奇怪的是,这个查询完全返回了每个城市的国家,然后我添加了城市关系的属性,因此这个问题颇具误导性的标题,对此感到抱歉:) –

+0

我看不出任何明显的东西,是吗?只是国家位?即如果你匹配'(用户:用户) - [:USER_HAS_HOME_CITY] - (homeCity:City)'是否返回正确的东西? –

+0

嗨小姐J.这里有一个查询示例的整个维基页面:https://github.com/Readify/Neo4jClient/wiki/cypher-examples –