Create a calculated field in my Entity

I need to add a read-only field that does not exist in my database table.

I want the field to show up in my entity but not saved to the database. It's a calculated field only.

I tried something like this:
[Transient]
Property Onhand: Double Read GetOnhand;

I must be missing something with the [Column] or maybe it cannot be done. Not finding anything in the docs.

And then what happened? You can do that.
There is just no need for a Transient attribute, they apply only to fields.

Gave me an error that the column was undefined.

I deleted the project so I don't have the exact error message anymore.

Appears there needs to be a column defined. Did not know how to do that because then it says the column is missing in the database table.

There is no obligation for entity classes to have all members mapped. Please provide minimal detailed information - the full class declaration, the exact error message, etc. so we can provide better information about what might be happening.

Hi Wagner,

I'm having the same problem. See details below. FullName is not appearing in Swagger.. The only difference I can see from examples in the manual is that my Entity is linked to a table. I am using NexusDB.

Thanks,

Ken

[Entity]
[Table('Clients')]
[Id('FGUID', TIdGenerator.Guid)]
TClients = class
private
  [Column('"GUID"', [TColumnProp.Required])]
  FGUID: TGuid;
  [Column('LastName', [], 30)]
  FLastName: Nullable<string>;
  [Column('FirstName', [], 20)]
  FFirstName: Nullable<string>;
  function GetFullName: string;
public
  property GUID: TGuid read FGUID write FGUID;
  property LastName: Nullable<string> read FLastName write FLastName;
  property FirstName: Nullable<string> read FFirstName write FFirstName;
  property FullName: String read GetFullName;
end;

function TClients.GetFullName:String;
begin
  Result:=FirstName.Value + ' ' + LastName.Value;
end;

Hi @Randall_Ken,

You must explicitly tell XData you want the property to be in JSON:

https://doc.tmssoftware.com/biz/xdata/guide/json.html#aurelius-entities

i.e., add [XDataProperty] attribute to the new property.

Thanks. I have made the following change but still can't see it in Swagger. Anything else missing?

   [XDataProperty]
    property FullName: String read GetFullName;

Do you see it in JSON response/request?
Have you also added XData.Model.Attributes unit to the uses clause?

1 Like

I was not using unit Xdata.model.Attributes. It's working when I include it.

1 Like

I suggest that your next book is XData for Idiots :grinning:

Sorry, one more question. In the function is it possible to access a value from an associated entity that uses lazy loading?

I have resolved this. I have to put any function last in the select otherwise the other entities values are not available.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.