Login Skip Navigation LinksWilsonORMapper > Forums Search
Demo Version Demo Version
Download and try for yourself a fully working demo version, including sample apps and documentation.  The only limitation is that the demo version only works inside the debugger.

PayPal Subscribe
Get It All for $50 USD:
WebPortal, ORMapper,
Source Code, All Updates
PayPal

User Login User Login
Log In
 
 
Reset Password

Wilson ORMapper Forums Wilson ORMapper Forums : Bugs & Issues : Delete from many to many with transactions and IObjectNotification

Date Post
8/27/2007 8:30:31 AM I tried to post this before, but it doesn't seem to have shown up, so I apologize if this post appears twice.

I have the following many to many relationship in the db:

ProductCategories (ProductCategoryId, fields)
ProductsToCategories (ProductCategoryId, ProductId)
Products (ProductId, fields)

In mappings.config, I have the following mappings:

    <entity type="Logic.Product" table="Products" keyMember="_productId" keyType="Auto">
        <attribute member="_productId" field="ProductId" alias="ProductId" oledbtype="Integer" cstype="int" vbtype="Integer" />
    </entity>
    <entity type="Logic.ProductCategory" table="ProductCategories" keyMember="_productCategoryId" keyType="Auto">
        <attribute member="_productCategoryId" field="ProductCategoryId" alias="ProductCategoryId" oledbtype="Integer" cstype="int" vbtype="Integer" />
        <relation relationship="ManyToMany" member="_products" field="ProductId"
             type="Logic.Product" alias="Products" lazyLoad="true"
             table="ProductsToCategories" sourceField="ProductCategoryId" destField="ProductId"
             />
    </entity>

I've used a slightly modified version of Paul Welter's CodeSmith templates to generate my classes with IObjectNotification. This generates the Products property correctly on the ProductCategory class.

When I delete a product, it does not get automatically deleted from ProductsToCategories. Is there a way to set it up to do this automatically?

In the meantime, I've implemented the IObjectNotification.OnDeleting method in Product.cs as follows:

        public void OnDeleting(Transaction transaction)
        {
            string opath = "Products[ProductId = ?]";
            OPathQuery<ProductCategory> query = new OPathQuery<ProductCategory>(opath);
            Collection<ProductCategory> categories = DataManager.ObjectSpace.GetCollection(query, ProductId);
            foreach(ProductCategory category in categories)
            {
                category.Products.Remove(this);
                category.SaveWithTransaction(transaction);
            }
        }


The problem is that the it seems that this adds this category save operation *after* the product delete operation in the transaction itself. But shouldn't any operations added to the transaction in OnDeleting (or OnUpdating and OnCreating, for that matter) be added first?

It seems like a minor change in order of operations, but I also don't know what other consequences this might have?

Is this a bug, or is it by design?

Thanks in advance,
Noam

8/27/2007 6:06:14 PM

Hi Noam:

The reason you're not getting deleting a product to automatically also delete productstocategories is because you don't have that relationship setup for products -- you only have it setup the other way.  As for the IObjectNotification event, you have the ability to do anything you want with the transaction that you are given, including creating and executing your own command.  On the other hand, if you simply pass that transaction on to some other "SaveWithTransaction" method, then if there's a questionable order to the sql operations then I would take a look at that "SaveWithTransaction" method.

Thanks, Paul Wilson

9/9/2007 8:01:16 AM Hi Paul,

Sorry it took me so long to respond.

Regarding the relationships, I was following the many-to-many relationship example I saw somewhere on this site (I think in a forum message), which only implemented the relationship one-way.

In any case, I fixed the problem by fixing the relationships in the database. It seems I forgot to cascade the deletions on both sides of the many-to-many table. Once I did that, everything worked very smoothly.

In fact, I was able to remove the OnDelete IObjectNotification implementation, since the database deletion takes care of the cascade (which is what I was really trying to enforce in the code).

Thanks,
Noam

btw, there's a problem with the html editor in firefox, where it seems to run some javascript loop endlessly. Firefox is able to monitor these infinite loops and abort them, but it's annoying because it takes some time for it to pick up on it.