Cascade delete in ORM 1 to N relationship

2 posts, 0 answers
  1. Jayavidya
    Jayavidya avatar
    14 posts
    Member since:
    Oct 2010

    Posted 12 Nov 2010 Link to this post

    Hello,
     I have a User class with a collection of User Group in it. When I delete the User, all the entries in the User Group should be deleted according to cascade delete. Please refer to the steps I followed.
    Step 1 : The usergroup collection has ItemType property.
    public partial class User
       { [Telerik.OpenAccess.FieldAlias("groups")]
           [Telerik.OpenAccess.ItemType(typeof(UserGroup))]
           public IList<UserGroup> UserGroups
           {
               get { return groups; }
           }
       }

    User orm has 

    [Telerik.OpenAccess.

     

    Depend()]

     

     

     

    private IList<UserGroup> groups = new List<UserGroup>();

     



    Step 2 : The app.config with User class details
    <class name="User">
      <field name="groups">
        <collection>
          <extension key="inverse" value="user" />
          <extension key="managed" value="true" />
        </collection>
      </field>
      <extension key="db-optimistic-locking" value="changed" />
      <extension key="db-key-generator" value="AUTOINC" />
    </class>

    Step 3: UserGroup with delete property
    <class name="UserGroup">
          <extension key="db-key-generator" value="AUTOINC" />
          <extension key="delete-orphans" value="true" />
    </class>

    Step 4: When I fetch the User using orm collection and give delete it does not work.It runs into some loop and doesnt end.
    User  deleteUser = (from c in ObjectScope.Extent<User>()
                                       where c.UserId == deleteUserId
                                   select c).First<User>();
                   ObjectScope.Remove(deleteUser);
                   ObjectScope.Transaction.Commit();

    Please advice me asap.
  2. Jayavidya
    Jayavidya avatar
    14 posts
    Member since:
    Oct 2010

    Posted 12 Nov 2010 Link to this post

    Hello,
    I got the solution. Please use the following code for the child object

    <class name="UserGroup">
    <field name="group">
                  <extension key="dependent" value="true" />
                  <collection element-type="User" />
    </field>
    </class>

    Thanks,
    JJ
  3. DevCraft banner
Back to Top