Key Properties
- 3 minutes to read
One of XAF’s functions is managing a database using an ORM tool. Dealing with the database assumes that each object has a property that allows you to distinguish an object among other objects of the same type. In relational databases, this property is called a Unique key (primary key or a key). Each business class you create with an ORM tool must have a key property that will be used as a database’s unique key. Key property implementation differs depending on the specific data access technology you use.
Entity Framework (EF)
When using EF, you can implement a key property in one of the following ways:
- Implement a property named “Id”.
- Implement a property that combines the class name and “Id”, such as “ContactId” (assuming that your class name is “Contact”).
- Decorate any property with the Key Attribute.
- Inherit your class from the DevExpress.Persistent.BaseImpl.EF.BaseObject class that has the Guid key property.
For detailed information, refer to the Keys Microsoft article.
Important
XAF’s EF Core implementation does not support the following data model configurations: entities with composite/compound keys (multiple properties as an entity key), keyless entity types, and owned entity types. In such scenarios, inherit your EF Core entity from XAF’s BaseObject
class or define your own class with a single Guid, numeric (Int32, Int64 ), or String primary key. You can also use non-persistent objects to integrate XAF UI with legacy systems.
XPO
In most XPO cases, you do not need to be concerned with the key property, because it is usually already implemented in a class that is used as the base for your own XPO business class. Refer to the Base Persistent Classes help topic for more information about different base classes to inherit from and their key properties.
If you need to implement your own key property, inherit your new class from one of the Base Persistent Classes without a key (XPLiteObject, XPBaseObject or XPCustomObject), implement a key property manually, then decorate it with the KeyAttribute.
Note
We do not recommend implementing composite or compound keys for new databases. While it is possible to design persistent classes for legacy databases with composite keys in certain scenarios, it is always better to modify the database schema to avoid this as using composite keys imposes some limitations on the default functionality. Refer to the How to create a persistent object for a database table with a compound key KB article to learn more. If you use compound keys in an ASP.NET Web Forms application, decorate the struct key property with the TypeConverterAttribute as described in the StructTypeConverter<T> class description. Also, note that the user-friendly URL mechanism does not support composite keys. Implement a custom ViewUrlManager to support them.