This question is locked. New answers and comments are not allowed.
Hello
I am facing the following issue:
We have class Company, with a list of Relations, that references 2 companies.
The idea is that if CompanyX has a relationship with CompanyY, that both of them have a list property 'Relations' with the same Relation in it, referencing one another.
The setup is as follows:
CompanyModel
01.public class CompanyModel02.{03. public int Id { get; set; }04. 05. public IList<RelationModel> Relations { get; set; }06. 07. public CompanyModel()08. {09. Relations = new List<RelationModel>();10. }11. 12. public void AddRelation(RelationModel relationModel)13. {14. if (Relations.Contains(relationModel)) return;15. 16. relationModel.From = this;17. relationModel.FromId = Id;18. 19. Relations.Add(relationModel);20. }21.}
01.private static MappingConfiguration<CompanyModel> GetCompanyModelMappingConfiguration()02.{03. var mapping = new MappingConfiguration<CompanyModel>();04. 05. mapping.MapType().UseDefaultMap().ToTable("Companies");06. 07. mapping.HasProperty(x => x.Id).IsIdentity(KeyGenerator.Autoinc);08.09. return mapping;10.}RelationModel
01.public class RelationModel02.{03. public int Id { get; set; }04. 05. public CompanyModel From { get; set; }06. public int FromId { get; set; }07. 08. public CompanyModel To { get; set; }09. public int ToId { get; set; }10.}
01.private static MappingConfiguration<RelationModel> GetRelationModelMappingConfiguration()02.{03. var mapping = new MappingConfiguration<RelationModel>();04. 05. mapping.MapType().UseDefaultMap().ToTable("Relations");06. 07. mapping.HasProperty(x => x.Id).IsIdentity(KeyGenerator.Autoinc);08. 09. mapping.HasAssociation(x => x.From)10. .WithOpposite(x => x.Relations)11. .ToColumn("FromId")12. .HasConstraint((x, y) => x.FromId == y.Id)13. .IsManaged();14. 15. mapping.HasAssociation(x => x.To)16. .WithOpposite(x => x.Relations)17. .ToColumn("ToId")18. .HasConstraint((x, z) => x.ToId == z.Id)19. .IsManaged();20. 21. return mapping;22.}The database holds a Relation record, FromId 2 (CompanyY), ToId 1 (CompanyX).
However, the list 'Relations' only gets filled on one of the two companies, whereas I'd expect both companies to have a filled Relations list.
What is happening? What am I doing wrong?
Thanks
Boris