Models in a Coalesce application are just EF Core POCOs. The properties defined on your models should fit within the constraints of EF Core.
Coalesce currently has a few more restrictions than what EF Core allows, but hopefully over time some of these restrictions can be relaxed as Coalesce grows in capability.
The following kinds of properties may be declared on your models.
- Primary Key
- To work with Coalesce, your model must have a single property for a primary key. By convention, this property should be named the same as your model class with
Idappended to that name, but you can also annotate a property with
[Key]to denote it as the primary key.
- Foreign Keys & Reference Navigation Properties
While a foreign key may be declared on your model using only the EF OnModuleBuilding method to specify its purpose, Coalesce won't know what the property is a key for. Therefore, foreign key properties should always be accompanied by a reference navigation property, and vice versa.
In cases where the foreign key is not named after the navigation property with
[ForeignKeyAttribute]may be used on either the key or the navigation property to denote the other property of the pair, in accordance with the recommendations set forth by EF Core's Modeling Guidelines.
- Collection Navigation Properties
- Collection navigation properties can be used in a straightforward manner. In the event where the inverse property on the other side of the relationship cannot be determined,
[InversePropertyAttribute]will need to be used. EF Core provides documentation on how to use this attribute. Errors will be displayed at generation time if an inverse property cannot be determined without the attribute. We recommend recommended that you declare the type of collection navigations as
- Non-mapped POCOs
Properties of a type that are not on your
DbContextwill also have corresponding properties generated on the TypeScript ViewModels typed as TypeScript External ViewModels, and the values of such properties will be sent with the object to the client when requested. Properties of this type will also be sent back to the server by the client when they are encountered (currently supported by the Vue Stack only).
See External Types for more information.
- Value Types
- Strings (although not actually a value type), any numeric type, enums, booleans, DateTimes, and DateTimeOffsets are all supported by Coalesce, as well as the nullable versions of all of these.
- Getter-only Properties
Any property that only has a getter will also have a corresponding property generated in the TypeScript ViewModels, but won't be sent back to the server during any save actions.
If such a property is defined as an auto-property, the
[NotMapped]attribute should be used to prevent EF Core from attempting to map such a property to your database.
For any of the kinds of properties outlined above, the following rules are applied:
- Coalesce provides a number of Attributes, and supports a number of other .NET attributes, that allow for further customization of your model.
- Properties will not be sent to the client and/or will be ignored if received by the client if authorization checks against any property-level Security Attributes present fail. This security is handled by the Generated C# DTOs.
- Loading & Serialization
- The Default Loading Behavior, any functionality defined in Data Sources, and [DtoIncludes] & [DtoExcludes] may also restrict which properties are sent to the client when requested.
- While Coalesce does not do anything special for the
[NotMapped]attribute, it is still and important attribute to keep in mind while building your model, as it prevents EF Core from doing anything with the property.