经过一番努力才能做到正确,我设法通过Neo4jClient将属性关系保存到Neo4j数据库。当我想要阅读这些关系时,问题就出现了。突然间,之前像魅力一样工作的查询不再返回我的用户了吗?没有异常被抛出,调用只是默默地返回空:(我读到了可能的反序列化问题,并将无参数构造函数添加到关系类但没有运气。
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 :(得分:3)
Neo4jClient正在逐渐停止使用Relationship
和Node
类,所以,好消息是 - 您不再需要将关系定义为: 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
作为输出。