Current filter:
                                You should refresh the page.
                                  • Description:
                                    Does XPO support tables, whose primary key is composed of several columns?

                                    Yes, XPO does support clustered key tables. You need to create a struct (Structure in VB.NET), which represents a multi-column key and an XPBaseObject derived class with a Key property definition. Here is the code for mapping to the Sales table in the MS SQL Pubs sample database:

                                    using System; using DevExpress.Xpo; using System.ComponentModel; public struct SalesKey { [Persistent("stor_id")] public string StorId {get; set;} [Persistent("ord_num")] public string OrdNum {get; set;} [Persistent("title_id")] public string TitleId {get; set;} } [Persistent("sales"), OptimisticLocking(false)] public class Sales : XPBaseObject { [Key, Persistent, Browsable(false)] public SalesKey Key {get; set;} [Persistent("payterms")] public string PayTerms {...} ... // other properties }
                                    Imports DevExpress.Xpo Public Structure SalesKey <Persistent("stor_id")> _ Public Property StorId As String <Persistent("ord_num")> _ Public Property OrdNum As String <Persistent("title_id")> _ Public Property TitleId As String End Structure <Persistent("sales"), OptimisticLocking(False)>_ Public Class Sale Inherits XPBaseObject <Key(), Persistent()> _ Public Property Key As SalesKey <Persistent("payterms")> _ Public Property PayTerms As String ... ' other properties End Class

                                    IMPORTANT NOTES

                                    1. Composite keys are supported for legacy databases only and using them imposes some limitations on the default functionality. You shouldn't use them in new XPO applications, because a) it is always possible to avoid this by changing your database schema; b) it often indicate bad database design. In most cases while developing a new XPO-based application, e.g., using an Object Oriented model, you won't need compound keys (this is true for other popular ORMs like Entity Framework). If you cannot avoid composite keys in your database and these limitations do not meet your needs, read and manipulate this data using ADO.NET means and present it using custom controls in XAF views: Using a Custom Control that is not Integrated by Default.
                                    2. Your custom key structure should contain properties and not fields for the correct operation of Windows Forms data-binding. Otherwise, the standard .NET Framework error 'Cannot bind to the property or column Name on the DataSource. Parameter name - dataMember'  may occur.
                                    3. Beware of the problem in .NET 4.0 related to the WinForms data-binding, which may result in an error when binding to complex properties. To avoid this error, you can use either the newest .NET 4.0 in-place update (automatically installed with Visual Studio 2012) where this problem is fixed, or you can create non-persistent wrapper-properties as shown below:
                                    using System; using DevExpress.Xpo; using System.ComponentModel; namespace YourProjectName { public struct TestKey { [Persistent, Browsable(false)] public string Key1 { get; set; } [Persistent, Browsable(false)] public string Key2 { get; set; } public TestKey(string key1, string key2) : this() { Key1 = key1; Key2 = key2; } } public class TestObject : XPLiteObject { public TestObject(Session session) : base(session) { } public override void AfterConstruction() { base.AfterConstruction(); Key = new TestKey("new value 1", "new value 2"); } [Key, Persistent, Browsable(false)] public TestKey Key { get; set; } [PersistentAlias("Key.Key1")] public string Key1 { get { return Convert.ToString(EvaluateAlias("Key1")); } //set { Key = new TestKey(value, Key2); } // If you need to edit the key parts. } [PersistentAlias("Key.Key2")] public string Key2 { get { return Convert.ToString(EvaluateAlias("Key2")); } //set { Key = new TestKey(Key1, value); } // If you need to edit the key parts. } } }
                                    4. Note that the ASPxGridView control requires additional adjustments to work with composite keys:
                                    ASPxGridView.KeyFieldName Property
                                    ASPxGridView.FindVisibleIndexByKeyValue Method
                                    5. We do not recommend including persistent objects to the composite key, as this design is not supported in some scenarios (e.g., when NestedUnitOfWork or Object Access Layer is used). If this is possible, store plain key values using simple type properties in the structure instead. See also: Support Persistent objects as a part of a composite key with NestedUnitOfWork.
                                    6. In the Middle Tier Security - WCF Service scenario, you should additionally register your structure type via the static WcfDataServerHelper.AddKnownType method. Call this method on the both server and client sides before the data server and client application are initialized:
                                    WcfDataServerHelper.AddKnownType(typeof(YourStructKeyType)); WcfDataServerHelper.AddKnownType(typeof(DevExpress.Xpo.Helpers.IdList));
                                    7. Starting with XAF v17.1.4, if you use business objects in an XAF application, decorate struct key properties with the TypeConverterAttribute with the StructTypeConverter type to enable built-in DevExpress.Web.ASPxGridView functions.
                                    8. You may receive the "The entity type 'XXX' does not have any key properties. Please make sure that one or more key properties are defined for this entity type. " error with OData Service (XpoDataServiceV3) if you map your persistent class to a table or view with a multi-column key. This configuration is not supported. Since the XAF Mobile UI uses XPO OData Service, this will not work there as well.

                                    See Also:
                                    How to: Map a Database View to a Persistent Class
                                    Basics of Creating Persistent Objects for Existing Data Tables
                                    When and Why XPO Extends the Database Schema
                                    How to prevent altering the legacy database schema when creating an XAF application
                                    How to define a multi-field unique index in a persistent object
                                    How to make user-friendly object identifiers

                                0 Solutions

                                Creation Date Importance Sort by