- 2 min to read
One of the XAF's functions is managing a database via 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, such 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 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 by 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
For detailed information, refer to the Code First Data Annotations MSDN article.
The DataView mode does not support business objects with composite keys in EF.
eXpress Persistent Objects (XPO)
In most XPO cases, you needn't be concerned with the key property, because usually it is already implemented in a class that is used as the base for your own XPO business class. Refer to the Base Persistent Classes article 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.
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 application, decorate the struct key property with the TypeConverterAttribute as described in the StructTypeConverter<T> article.