Ticket Q472391
Visible to All Users

Composite primary key and associations using xpo

created 12 years ago

Hi

I'm actually working on the remake of an old vb6 application module into C# using Xafml and XPO linked to Oracle 11g DB; it’s for us the ultimate test of whether DevExpress functionalities and extendabilitywill fulfill our needs.

My problem:

There is no way to modify database table’s structure since there are too many VB6 projects depending on the actual schema.Many tables have composite primary keys and these are associated. Since we can’t add a unique primary key to these tables, I have to figure out how to work using these associations with DevExpress.

Here’s a basic example of what we’re facing :

See schema_table.png

Yet, we know we can add a new EMPRUNT to the database using the NEW button; LIVRES and HUMAIN belonging to the same Unit of Work (or session) as the main saving window.

See image1.png

The problem occurs when we are in a HUMAIN window, and try to add an EMPRUNT from that point.

See image2.png and image3.png

When we click on the save button, nothing special happens that let me know a problem occurred.So when I get back to the HUMAIN detail view window, the save button gets activated, and to have the application to be coherent, I should see the new EMPRUNT in the bottom grid, which does not appear at this time.
Since the save button gets enabled, we click on it and this error is triggered:

See image4.png

The same problem occurs in HUMAIN when we go through the LIVRES detail view window.

See image5.png

Yet, I have a partial solution, which consists in removing the NEW EMPRUNT button in the EMPRUNT collection displayed in a LIVRE or HUMAIN detail view and allow the user to add new EMPRUNT using the NewItemRowPosition property so the user does not leave the window and, therefore, does not face a UnitOfWork change (or session).

See image6.png

We also imagined to overload the save button event, but since we cannot see what happens in the base OnSave method, there is no way to change any behavior in it…
At last, you have to understand we started to use DevExpress in early 2012 to fasten our new modules and applications developments as well as our UI conception. After so many days (multiplied by hours…) of testing, reading, searching and discouraging efforts on such a simple thing, we turn to you and your expertise to give us a guideline, some encouraging hints or some code samples to achieve our goal.Or a sound advice like "Simply drop DevExpress and get back at doing things in plain C#"…

Thanks
Michael

Answers approved by DevExpress Support

created 12 years ago (modified 12 years ago)

Hi Michael.
Thank you for contacting us. I am afraid the scenario you are implementing is not supported (see ticket Support Persistent objects as a part of a composite key with NestedUnitOfWork). Particularly, the new Emprunt object is not added to the collection and an exception is thrown when you try to save the Humain object, because our framework does not expect a persistent object within a key and thus, does not update key members by reloading referenced object in the Humain object's Session/ObjectSpace when you save the Emprunt object.
In addition, since XPO uses the primary key to identify objects, it is forbidden to change the primary key value or its components after the object was committed to the database. Thus, I suggest that you prevent reference property setters in the Emprunt class from changing the key if Session.IsNewObject(this) returns True. You can hide or disable corresponding property editors using the Conditional Appearance module as suggested in ticket Conditional Appearance rule for a new object.
I recommend that you remove associations and programmatically create related collections and load referenced objects by their keys. Attached is a modified code.

    Comments (2)

      Is there any workaround to use Associations with Compound Keys ?

      DevExpress Support Team 10 years ago

        @bbirbo: There's no general solution I'm afraid. If you need our assistance with a specific scenario, please create a new ticket and describe your requirements in detail. We will do our best to help you.

        Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

        Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.