Current filter:
                                You should refresh the page.
                                  • Description:
                                    I wish to use text values for the object identifiers instead of auto-generated numbers. A code of letters and numbers is easy to understand and remember. How to redefine the ID of a persistent object?

                                    To change the name and/or data type of an object's key field, you should inherit your persistent object from the class that does not have a built-in key property, and define a field with the Key attribute.

                                    public class MyObject : XPCustomObject { [Key(AutoGenerate = true)] public Guid UniqueIdentifier; //... }

                                    Among persistent classes defined in the XPO library, only the XPObject class has a built-in key property. Please see the XPO Classes Comparison help topic for reference.
                                    We recommend that you don't fill key values with any meaning and don't display them to end users. If an object's ID carries business logic of any kind, this may cause problems for you as a programmer in the future. For instance, your clients may require the generating algorithm for object IDs to be changed or want to re-number the records in an existing database. It may be very difficult if not impossible to convert old IDs into new ones.
                                    You may think about the XPObject.Oid as if it were a memory address pointer to an object in C++ or Delphi. It doesn't make sense to show a pointer to users. The operations performed against pointers are usually limited to comparisons to see if they are equal or to get an object by a given address. The same is true of the XPObject.Oid. You can use it to compare objects to see if they are the same object and take an object from storage via the Session.GetObjectByKey method.
                                    Therefore, it's best to define a special field/property which can be seen and used by the users of your application to identify business objects (documents). It will be easier for you to implement any changes later on for such an "ID".
                                    You will need to use the Session.FindObject method to obtain an object by a user-friendly ID. To optimize searching for objects, declare the ID field with the Indexed attribute.
                                    Here is some sample code:

                                    using DevExpress.Xpo; // Object with a user-friendly ID public class Client : XPObject { [Indexed(Unique=true)] public string ClientID; //... } // Add object Client client = new Client(); client.ClientID = "A007"; client.Save(); // Get object Client GetClientById(string clientID) { return Session.DefaultSession.FindObject<Client>(new BinaryOperator("ClientID", clientID)); } Client client = GetClientById("A007"); Console.WriteLine(client == null ? "Client not found" : client.ClientID);

                                    The attached example provides an XpoSequencer helper class. Use the XpoSequencer.GetNextValue function to generate unique user-friendly IDs for your objects. Similar code is used in our Support Center to generate IDs for questions and bug reports.
                                    See Also:
                                    A2226 - How to define a read-only persistent property
                                    A2712 - How to define a multi-field unique index in a persistent object

                                0 Solutions

                                Creation Date Importance Sort by