3

In the Northwind example's Customer DTO there is a field Email that is synthetic -- i.e. it doesn't come from the database. Code here: https://github.com/ServiceStackApps/Northwind/blob/master/src/Northwind/Northwind.ServiceModel/Types/Customer.cs

But when viewing this in the running example, this field is not visible: http://northwind.servicestack.net/query/customers

I've noticed this DTO has [DataContract] and [DataMember] annotations, whereas most other examples do not.

How do I add synthetic fields to an AutoQuery? Just adding one with a pupulated getter gives me an error, as ServiceStack tries to fetch it from the database. Is there an "Ignore" annotation that should be used? Or would the best way be to separate the "DTO-for-database" and the "DTO-for-the-service", and use AutoMapper between them somehow?

specimen
  • 1,735
  • 14
  • 23

1 Answers1

2

Is there an "Ignore" annotation that should be used?

Exactly that, use [Ignore] to ignore the field from being used in OrmLite or AutoQuery whilst [IgnoreDataMember] is when you want to ignore the property in serialization.

The Email field doesn't get shown because it doesn't have a [DataMember] field in a [DataContract] class which is one of the ways to ignore fields in Serialization with ServiceStack.Text.

mythz
  • 141,670
  • 29
  • 246
  • 390
  • Just to confirm: the `[Ignore]` applies between SS and database (ORM), whereas `[IgnoreDataMember]` is between SS and clients (the DTO)? – specimen Mar 22 '19 at 08:53
  • 1
    @specimen there’s no between, it’s simply [Ignore] is ignored by OrmLite (which AutoQuery uses) and [IgnoreDataMember] is ignored by Serializers. – mythz Mar 22 '19 at 09:06
  • So I added an entry like this: `[Ignore] public string Hello { get { return "Hello there"; } }` and then AutoQuery fails. SQL is correct now, but: `System.InvalidOperationException: Sequence contains no matching element at System.Linq.Enumerable.First` -- I'm trying to add a calculated field which should be returned to the client, but doesn't exist in DB. – specimen Mar 22 '19 at 15:31
  • @specimen please open a new question with the full AutoQuery DTO you’re using and contains the full Exception StackTrace. – mythz Mar 22 '19 at 15:50