Can't create managed one-to-many collection

18 posts, 0 answers
  1. Denis Vulinovich
    Denis Vulinovich avatar
    29 posts
    Member since:
    Jun 2009

    Posted 12 Aug 2009 Link to this post

    I'm trying to use the Forward Mapping wizard to create a managed one-to-many relationship between Company (one) and Person (many) objects. But the "Managed collection" option on the wizard is disabled and I can't select it.

    Here's the relevant code from the Company and Person classes:
        [Telerik.OpenAccess.Persistent()] 
        public class Company 
        { 
            private IList<Person> _persons = new List<Person>(); 
     
            [FieldAlias( "_persons" )]
            public IList<Person> Persons
            {
                get { return _persons; }
            }

           public void AddPerson( Person person ) 
            { 
                _persons.Add( person ); 
            } 
     
            public void RemovePerson( Person person ) 
            { 
                _persons.Remove( person ); 
            } 
        } 
     
        [Telerik.OpenAccess.Persistent()] 
        public class Person 
        { 
            private Company _company; 

            [FieldAlias( "_company" )]
            public Company Company
            {
                get { return _company; }
            }
        } 
     
    In the database it creates a company_person table with the id from each class, and it updates this table whenever I call the Company's AddPerson() and RemovePerson() methods. But the Person's _company field is always null.

    Here are the relevant parts of App.config:
              <class name="Company"
                <field name="_persons"
                  <collection> 
                    <extension key="db-link-table" /> 
                  </collection> 
                </field> 
              </class> 
              <class name="Person"
     
              </class> 
     
     
    It's interesting that the Person class has no key for its _company field.

    I've tried disabling OpenAccess in this project, deleting App.config and then starting from scratch again, but I still get the same result.
  2. Denis Vulinovich
    Denis Vulinovich avatar
    29 posts
    Member since:
    Jun 2009

    Posted 13 Aug 2009 Link to this post

    I've just noticed that the help topic "Forward Mapping Wizard - One to Many Reference Mapping - Without Join Table" shows a screen shot with a "Managed Collection" checkbox just below "Default fetch group". But my software doesn't have that option. I'm using Express version 2009.2.701.5.
  3. Jan Blessenohl
    Admin
    Jan Blessenohl avatar
    707 posts

    Posted 13 Aug 2009 Link to this post

    Hi Denis Vulinovich,
    The _persons collection is mapped to a join table by default. It has nothing to do with the reference _company. You have to tell OpenAccess that this is a 1:n and that _company is the 1 part.

    To do so please open the forward mapping dialog and select Company->_persons node. Now switch off the Join Table check boy and select _company in the Inverse Field ComboBox. Now you can switch on Manager Collection and it will work.



    Sincerely yours,
    Jan Blessenohl
    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.
  4. Denis Vulinovich
    Denis Vulinovich avatar
    29 posts
    Member since:
    Jun 2009

    Posted 13 Aug 2009 Link to this post

    Thanks, Jan, that worked.

    With my previous comment, I was actually looking at a different pair of classes where the "n" class didn't have the reverse field. So that's probably why the Managed Collection checkbox didn't appear.

    Thanks again for your help.

    Denis
  5. Iftekhar Ivaan
    Iftekhar Ivaan avatar
    17 posts
    Member since:
    Apr 2006

    Posted 16 Aug 2009 Link to this post

    Hi,

    If I do this, it works fine. But in the database I do not see and one to many link in the database diagrame view. It does not shows any link between two tables. If we we many to many we can see the link. I am using SQL server 2005.

    How can I see the link in the database diagrame. It is easy to understand the table relation.

    Thanks
  6. IT-Als
    IT-Als avatar
    381 posts
    Member since:
    Sep 2008

    Posted 17 Aug 2009 Link to this post

    Hi Iftekhar,

    Please check if you on the many side of the association have one of the "Generate foreign key...." checked. Doing so, will get you a foreign key contraint on the many side table that references the one side table.
    And foreign key constraints should be recognizable in the various tools

    Regards

    Henrik
  7. Iftekhar Ivaan
    Iftekhar Ivaan avatar
    17 posts
    Member since:
    Apr 2006

    Posted 17 Aug 2009 Link to this post

    Hi,

    I have done that and it is not still working.
  8. PetarP
    Admin
    PetarP avatar
    754 posts

    Posted 18 Aug 2009 Link to this post

    Hello Iftekhar Ivaan,

    By default Telerik OpenAccess ORM uses algorithms to decide weather a foreign key constraint makes sense or not. In your case you can use this setting in the backend configuration to manually override our algorithm:
    <dbNullForeignKey>true</dbNullForeignKey> 
    This will create your foreign key constraint and you will see the connection in your diagram. Let us know if the following solution works for you.

    Sincerely yours,
    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.
  9. Iftekhar Ivaan
    Iftekhar Ivaan avatar
    17 posts
    Member since:
    Apr 2006

    Posted 18 Aug 2009 Link to this post

    Hi,

    Which section I need to copy this.

    Thanks
  10. PetarP
    Admin
    PetarP avatar
    754 posts

    Posted 19 Aug 2009 Link to this post

    Hi Iftekhar Ivaan,

    In the backend configuration node that is located in the app.config file. To give you more clear example it should look something like this:
     <backendconfigurations> 
          <backendconfiguration id="mssqlConfiguration" backend="mssql"
            <dbNullForeignKey>true</dbNullForeignKey> 
            <mappingname>mssqlMapping</mappingname> 
          </backendconfiguration> 
        </backendconfigurations> 


    Best wishes,
    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.
  11. Denis Vulinovich
    Denis Vulinovich avatar
    29 posts
    Member since:
    Jun 2009

    Posted 19 Aug 2009 Link to this post

    I have another problem with my original managed one-to-many relationship between Company and Person objects. In the Forward Mapping wizard, on the Person._company field I've selected the option "Throw an exception on commit if field is null", but the database doesn't have a NOT NULL constraint on the same column. If I add one, it's deleted the next time I build the project.

    It's only happened since I've added both classes to an inheritance hierarchy where they both derive from the same base class. This is from App.config:
              <class name="Company">
                <extension key="db-inheritance" value="vertical" />
                <extension key="db-table-name" value="company" />
                <field name="_persons">
                  <collection>
                    <extension key="inverse" value="_company" />
                    <extension key="managed" value="true" />
                  </collection>
                </field>
              </class>
              <class name="Person">
                <field name="_company" null-value="exception">
                  <extension key="db-column">
                    <extension key="db-column-name" value="Company" />
                  </extension>
                </field>
                <extension key="db-inheritance" value="vertical" />




              </class> 

     

  12. Zoran
    Admin
    Zoran avatar
    534 posts

    Posted 24 Aug 2009 Link to this post

    Hello Denis Vulinovich,

    This is a result of the way Telerik OpenAccess ORM handles these scenarios. When you have a field which has the "Throw an exception on commit if field is null" option switched on, the runtime makes the check in-memory and does not hit the database server at all before throwing the exception. That is why this constraint is not visible in the database.

    Regards,
    Zoran
    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.
  13. Denis Vulinovich
    Denis Vulinovich avatar
    29 posts
    Member since:
    Jun 2009

    Posted 24 Aug 2009 Link to this post

    Hi Zoran,

    Since I derived Person and Company from another class, my tests have been failing because I'm no longer getting the Telerik.OpenAccess.Exceptions.DataStoreException that it used to throw when a Person was created without a Company. That's when I discovered the missing database constraint.

    Regards,
    Denis
  14. PetarP
    Admin
    PetarP avatar
    754 posts

    Posted 27 Aug 2009 Link to this post

    Hello Denis Vulinovich,

    When you specify a field to "Throw an exception on commit if field is null" no constrains are created in the database but the field is being checked at runtime. If the field is null, the object is not stored but an exception is thrown. It seems we have some runtime problems with this feature and that is why you are not seeing the exception. Please find your Telerik points updated for reporting this bug.

    Greetings,
    Petar
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  15. Denis Vulinovich
    Denis Vulinovich avatar
    29 posts
    Member since:
    Jun 2009

    Posted 31 Dec 2009 Link to this post

    Hi Petar,

    I'm just wondering when this bug is going to be fixed?

    I'm using OpenAccess version 2009.3.1104.2, and no exception is thrown when the field is null.

    Thanks,
    denis
  16. PetarP
    Admin
    PetarP avatar
    754 posts

    Posted 05 Jan 2010 Link to this post

    Hello Denis Vulinovich,

    You can set this option via the wizard only for forward mapping scenarios. The equivalent of this constraint in reverse mapping would be if a column is marked as non nullable. The check itself used to be performed in-memory but now it is pushed to the server.

    Regards,
    Petar
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  17. Denis Vulinovich
    Denis Vulinovich avatar
    29 posts
    Member since:
    Jun 2009

    Posted 07 Jan 2010 Link to this post

    Hi Petar,

    I'm using the Forward Mapping wizard and I select the option to throw an exception if the field is null, as shown in attached screen dump. But I get no exception if I create a Person object without a Company.

    Regards,
    Denis
  18. PetarP
    Admin
    PetarP avatar
    754 posts

    Posted 08 Jan 2010 Link to this post

    Hello Denis,

    Was your model originally forward mapped or it was reverse mapped? There is a good possibility that the data in your column is in conflict with the constraint we are trying to create and that is why the constraint is missing even after you check the box. Can you see if there are entries in your database that have null value for the  company column? If so, please change them to non-null values and try again. This should solve your problem.

    Sincerely yours,
    Petar
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Back to Top