Self-referencing association

Thread is closed for posting
2 posts, 0 answers
  1. Jay
    Jay avatar
    28 posts
    Member since:
    Apr 2006

    Posted 27 Aug 2010 Link to this post

    Is it possible to use the designer to created a self-referencing association (tree structure) using the designer?  For example, I'd like to have a Category class that has sub-categories, each in turn having sub-categories and so on.
  2. Petko_I
    Petko_I avatar
    184 posts

    Posted 01 Sep 2010 Link to this post

    Hello Jay,

    I will tell you what steps to follow in order to create a hierarchy. Right now we have a flaw in the automation of creating self-referencing associations and that is why I will give you guidelines where some extra effort is needed.

    With the official release Q2 version of OpenAccess (2010.2.714.1) forward mapping an association requires having underlying tables mapped to the entities participating in the relationship. There is a blog post which elaborates on this scenario. Let’s repeat briefly the basic steps (as described in the blog) for creating a Category hierarchy.  Just do not forget to rebuild your project before running our wizards.

    1.Create an entity by dragging a MetaClass to the design surface, rename it to Category and add the properties CategoryID, CategoryName, ParentCategoryID.

    2.Update the database from your model by running the Update Schema Wizard.

    3.Update the model from the database by adding the newly generated table.

    4.Map the table to the Category entity with the help of the Mapping Details Editor.

    5.Drag an association from the entity to itself.

    6.In the Define Relationship dialog specify ParentCategoryID as the Owner End and CategoryID as the Target End.

    7.You should now be able to see the association properties in the entity diagram – the Category instance property and a list of categories. Our naming strategy assigns default names for the references. In most cases the generated names are unsuitable and that is why you should rename the Category reference property to ParentCategory, for example.

    8.This step here is where the problem comes. In general, when you run the Update database from model tool with an option for a schema migration script a foreign key constraint should be available in the SQL. However, the self referencing is not detected (we will definitely fix this for the next distribution of OpenAccess), so you may execute manually the following script.

    ALTER TABLE Category ADD CONSTRAINT FK_Category_Categories FOREIGN KEY(ParentCategoryID)
    REFERENCES Category(CategoryID);

    9.The final step is to update the model from the database. Right-click the Model Schema Explorer, select Update From Database, right-click the Category table which should display a status of Modify and choose Apply Selected.

    With the internal build version of OpenAccess (2010.2.804.5) the steps are fewer. When you create the entity, you can drag an association to it right away. During the Update database from model phase the self referencing constraint is still not detected, so executing the Alter table statement should be done manually again. After updating the model from the database and specifying the mapping with the Mapping Details Editor, the same editor should be used to perform step 6 of the previous sequence. On completing this task you should have a working hierarchy defined.

    Do not hesitate to contact us for further guidance.

    Best wishes,
    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
Back to Top