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

Managed many-to-many relationship not filling link table

8 Answers 179 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Denis Vulinovich
Top achievements
Rank 1
Denis Vulinovich asked on 19 Jul 2009, 05:27 AM
I'm trying to set up a managed many-to-many relationship between classes Project and Company. OpenAccess creates two link tables - project_company and company_project - but a row is only inserted into one of them.

Here are relevant parts of my code:

    [Persistent()]
    public class Project
    {
        [ItemType( typeof( Company ))]
        private IList _companies = new ArrayList();

        public void AddCompany( Company company )
        {
            _companies.Add( company );
        }
    }

    [Persistent()]
    public class Company
    {
        [ItemType( typeof( Project ) )]
        private IList _projects = new ArrayList();

        public void AddProject( Project project )
        {
            _projects.Add( project );
        }
    }

// From App.config:

          <class name="Project">
            <field name="_companies">
              <collection>
                <extension key="inverse" value="_projects" />
              </collection>
            </field>
          </class>
          <class name="Company">
            <field name="_projects">
              <collection>
                <extension key="db-link-table" />
                <extension key="managed" value="true" />
              </collection>
            </field>
          </class>

// From my test class:

        [Test]
        public void TestManyToMany()
        {
            using ( IObjectScope scope = ObjectScopeProvider1.GetNewObjectScope() )
            {
                scope.Transaction.Begin();
                
                Project project = new Project( "Project Name", "Project Description" );
                scope.Add( project );
                
                Company company = new Company( "Company Name" );
                scope.Add( company );

                project.AddCompany( company );
                //scope.Add( project );     // I tried these lines but they didn't help
                //scope.Add( company );
                
                scope.Transaction.Commit();
            }
        }



When I run the test code, I get a row in the company_project table, but nothing in project_company.

8 Answers, 1 is accepted

Sort by
0
PetarP
Telerik team
answered on 22 Jul 2009, 04:06 PM
Hi Denis Vulinovich,
Telerik OpenAccess ORM supports many-to-many mappings where two collections share a common link table. Having this in mind you need only one link table(but you have two). To get two link tables you must have left the 1:n option checked in the forward mapping wizard. Because the many-to-many mapping needs only one table you get a row inserted in it and nothing is inserted in the other table.

Kind regards,
Petar
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Denis Vulinovich
Top achievements
Rank 1
answered on 23 Jul 2009, 08:04 AM
Hi Petar,

Thanks for your reply. I had actually selected m:n in the Forward Mapping wizard. I've just tried it again and it's done the same thing.

When the "m:n Collection" options appeared in the wizard, I selected  "Managed collection" in addition to the "Preserver order" option that was already selected. First I did this for the Project._companies field, and when I looked at the Company._projects field it already had the same settings. Then I built the project and looked in the database. It had both project_company and company_project tables, even though I had previously deleted them.
0
PetarP
Telerik team
answered on 29 Jul 2009, 06:13 AM
Hi Denis Vulinovich,

This is really strange. Usually when you chose m:n collection you should see that both Project_companies and Company_projects are mapped to one table in the SQL server. Having this in mind they should both correspond to the same table. Since you are getting one record in one table than everything works fine. The second table must be created because of some old setting left from previous configuration.

Regards,
Petar
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Denis Vulinovich
Top achievements
Rank 1
answered on 29 Jul 2009, 07:18 AM
Hi Petar,

I've got it working now. I disabled the project from using OpenAccess, removed all OpenAccess References and deleted App.config. Then I re-enabled the project and set up the m:n relationship again, and now my database only has a single company_project table. But the fields in App.config still look the same as they were before.

Thanks for your help.

Regards,
Denis
0
Jdorak
Top achievements
Rank 2
answered on 22 May 2010, 02:55 AM
      
             project.AddCompany( company ); 

I was having the same issue today and found this thread, unfortunately there is no solution pointed in here. Anyways after many hours scratching my head I managed to figure it out on my own. The reason the record in the link table is not getting inserted in the above line is because on your mapping if you look carefully is the "Company" class and not the "Project" class the owner of the many-to-many relationship.

company.AddProject( project ); 
The above line works fine and does insert the new record in the link table.

Btw you did not need to create a method to add a new item to the company List in the project class, since is a List you can just go ahead and use the Add() method.

Now in my case I checked the App.config and did not fund the line:
<extension key="managed" value="true" /> 
Is there any reason why is not there? (I am reverse mapping btw).

Here's the code from my App.config (I am doing a many to many relationship between USER and ROLE classes and the link table is called USERROLE which only has the PK of both classes).


In the ROLES class (which is the owner of the relationship) I have:

            <field name="users">  
              <collection> 
                <extension key="db-link-table">  
                  <extension key="db-table-name" value="'UserRole'" /> 
                  <extension key="db-owner-ref" null-value="exception">  
                    <extension key="db-column">  
                      <extension key="db-type" value="INTEGER" /> 
                      <extension key="db-column-name" value="RoleID" /> 
                    </extension> 
                  </extension> 
                  <extension key="db-value" null-value="exception">  
                    <extension key="db-column">  
                      <extension key="db-type" value="INTEGER" /> 
                      <extension key="db-column-name" value="UserID" /> 
                    </extension> 
                  </extension> 
                </extension> 
                <extension key="ordered" value="false" /> 
              </collection> 
            </field> 


In the USERS class I only get:
            <field name="roles">  
              <collection> 
                <extension key="inverse" value="users" /> 
              </collection> 
            </field> 

So when trying to add a role to a user the link table does not get inserted, but when adding a user to a role then it works like a charm. Is this normal behaviour?


0
Ady
Telerik team
answered on 26 May 2010, 09:52 AM
Hi rtelles,

 The 'Managed' setting can be set using the Forward mapping wizard even if you have created your classes using the Reverse mapping wizard. This setting is not available in the Reverse mapping wizard.
Can you specify this setting and verify the behavior?

Regards,
Ady
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Jdorak
Top achievements
Rank 2
answered on 03 Jun 2010, 01:25 AM
I did try tp put the option manually but makes no difference whatsoever. However keep in mind my project is working fine without it, I was just wondering what that option was for.

Thanks for the reply.
0
Ady
Telerik team
answered on 04 Jun 2010, 11:54 AM
Hello rtelles,

The 'managed collection' setting specifies that when one side of the relationship is changed automatically the other side is updated.
Can you successfully add items to the M:N collection from either side now?

Greetings,
Ady
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Tags
General Discussions
Asked by
Denis Vulinovich
Top achievements
Rank 1
Answers by
PetarP
Telerik team
Denis Vulinovich
Top achievements
Rank 1
Jdorak
Top achievements
Rank 2
Ady
Telerik team
Share this question
or