Generated Model Only Property

Posts   
 
    
Posts: 37
Joined: 09-Nov-2016
# Posted on: 12-Jun-2018 17:25:04   

Hi,

I am using LLBLGen 5.4 and LLBLGen Runtime Pro Framework in a .NET Standard 2.0 Class Library.

I am transferring data to ElasticSearch and I need to know, what the object type originally was before it was projected to a Dto, so I can store the object type in ElasticSearch and also return the object type along with the other properties through my WebApi.

For example I have an Entity called CarEntity and a Dto called CarDto. I would like the CarDto to have a property called ObjectType which should contain the value "CarEntity". This property should be Model Only.

I have creating a partial class for the CarDto that contains an ObjectType Property and added the following code to the CreateProjectionFunc method in CarDtoPersistence class:


// __LLBLGENPRO_USER_CODE_REGION_START ProjectionRegion_CarDto

ObjectType = p__0.GetType().Name,

// __LLBLGENPRO_USER_CODE_REGION_END 

However there are two problems with this approach:

1: The CarDto does have the correct ObjectType property when I look at the Object in Visual Studio, however it does not get returned in a WebAPI call. It looks like the property gets lost when you serialize the Dto. Is there any way to force this property to be serialized as well?

2: I need to create a partial class for each Dto I want to add this property to. Is there an Dto equivalent of CommonEntityBase that I can extend instead?

Or am I going about this the completely wrong way?

Best regards, Andreas

Walaa avatar
Walaa
Support Team
Posts: 14946
Joined: 21-Aug-2005
# Posted on: 12-Jun-2018 20:02:01   

1: The CarDto does have the correct ObjectType property when I look at the Object in Visual Studio, however it does not get returned in a WebAPI call. It looks like the property gets lost when you serialize the Dto. Is there any way to force this property to be serialized as well?

Do you have the "DataMember" attribute set to the property?

Posts: 37
Joined: 09-Nov-2016
# Posted on: 12-Jun-2018 22:03:05   

Hi Walaa,

No, I forgot that one. I am at home right now, so I can't test it, but I am sure that it is. Thank you.

Is there a way to avoid creating all the extra partial classes?

Best regards Andreas

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 13-Jun-2018 09:09:46   

Jordbaerhjelmen wrote:

Is there a way to avoid creating all the extra partial classes?

Yes, Go to** Project->Settings->Derived Mode**l, select your derived model, click on DTO Class Model, and use the setting Root Derived Element base class name. Then all your DTOs will be extending that base class that you should write with your custom stuff.

David Elizondo | LLBLGen Support Team
Posts: 37
Joined: 09-Nov-2016
# Posted on: 13-Jun-2018 10:57:02   

Perfect! Everything works, thank you for the quick response.