This is a migrated thread and some comments may be shown as answers.

many-to-many relations

3 Answers 100 Views
LINQ (LINQ specific questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Filipe Peixinho
Top achievements
Rank 1
Filipe Peixinho asked on 15 Apr 2010, 05:37 PM
In a small sample application I have a managed many-to-many relation between classes A and B. In which A contains a IList<B> and B contains a IList<A>.

This relation is translated in database by 3 tables, A, B and A_B, A_B being the relation table, containing both IDs of A and B. Nothing strange so far.

After creating a few objects of types A and B I checked the DB tables and everything was correct. A and B tables contained the new objects and A_B contained relations between all of those, which is also correct.

The problem is that when loading an object of type A its property IList<B> is null, the same happens on the opposite direction, if I load an object of type B its IList<A> is null. I though this could be a simple fetch group problem, but if I refer to any of the collection properties in a LINQ query, an exception is thrown and its message leads me to believe that it is due to the collection reference.

Is there any known issue with many-to-many relations or any aspect I may have overlooked on its implementation?

3 Answers, 1 is accepted

Sort by
0
IT-Als
Top achievements
Rank 1
answered on 16 Apr 2010, 12:53 AM
Hi Filipe,

Do you initialize the list on both sides of the m:n relationsship like:

class A
{
   private IList<B> bList = new List<B>();
}

and likewise for the class B, just with a list of A's instead?

Regards

Henrik
0
Filipe Peixinho
Top achievements
Rank 1
answered on 16 Apr 2010, 03:23 PM
Hello Henrik, thank you for quick reply.
 
I really hadn’t initialized the collection in the class declaration as you suggest in your reply. I didn’t think it necessary, because I usually don’t do it for 1 to many relations and the ORM works fine. Still, I tried it and the behavior was the same.
I then started to test further and deeper and came to an odd conclusion: 

    - As this was a simple testing sample, I didn’t follow all coding best practices, I did not encapsulate the class variables, making them public instead. As a result I got the following: 
  • If the relation collection was not managed, even if the relation was built by adding new objects on both class A and Class B IList<> variables, no records were created in the relation table and obviously, no records were read when loading objects of type A or B from DB 
    If the relation collection was managed, the relation records are created in the relation table, but not loaded when loading objects of types A or B, i.e., when loading an object of type A, its IList<B> variable would not be filled, keeping the value null 
  • When I redid my project and encapsulated all variables of classes A and B, using FieldAlias to associate the properties with the private variables everything worked as I originally intended.

This result leads me to believe that unless there was something wrong with my tests, there may be some undocumented feature (AKA bug) in OpenAccess ORM when manipulating many-to-many relations between classes that do not contain encapsulated variables, i.e., classes in which all variables are public as opposed to classes in which all variables area private and made available through public properties.

Best regards,

0
IT-Als
Top achievements
Rank 1
answered on 17 Apr 2010, 01:42 PM
Hi Filipe,

Glad you made it work.

I must admit I have never tried only using public properties, since in my classes are public properties are always backed with a private field.
However, the OpenAccess engine actually (believe it or not) work on the fields of a class (private or not) and not the properties. When you do OQL you query a class by its field names instead of the property names.

This might have resulted in the behaviour you have seen... since you only had properties and not fields in you class?

Regards

Henrik
Tags
LINQ (LINQ specific questions)
Asked by
Filipe Peixinho
Top achievements
Rank 1
Answers by
IT-Als
Top achievements
Rank 1
Filipe Peixinho
Top achievements
Rank 1
Share this question
or