Adding field dinamically to entity

Posts   
 
    
mma02
User
Posts: 26
Joined: 04-Aug-2009
# Posted on: 13-Dec-2009 16:39:43   

Hi,

we need to add a field to an entity at run time, the reasone is that we need to bind this entity to a grid and we need to display this extra column which is a summery of data that is available at runtime.

what we did is inherrite the entity we want to add the field to, added the property and added the field to the Fields collection.

we can build and populate the entity, pass it to the client and bind it to the grid.

when we try to save the updated entity, we get the following exception:

The element '<The name of the new field>' isn't found as a property of entity '<the name of the base entity>'

it seems that the serialization code recognizes the entity to be serialized as the base entity and not the drived class.

when debugging, i see that the type of the entity being sent is the right one.

i have traced the wcf message's body content and here it is:


<UnrecognuzedDeposits z:Type="Project.Namespace.UnrecognuzedDeposits" z:Assembly="Project.Namespace, Version=2.0.16.2, Culture=neutral, PublicKeyToken=null" xmlns="http://schemas.datacontract.org/2004/07/Project.Namespace" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/">
- <HfkdInputTransactStorageEntity ObjectID="c69c269f-2bfd-47b7-97a1-e385524dbf6a" Format="Compact25">
  <TransactionId>22816773</TransactionId> 
  <AdmissionDate>2009-09-04T00:00:00</AdmissionDate> 
  <TransactionSource>2</TransactionSource> 

  <SumToSplit>500</SumToSplit> 
  <_lps fs="FRUVFEBVABUAAAAAAEEBAAUABFBA" es="0" /> 
  </HfkdInputTransactStorageEntity>
  </UnrecognuzedDeposits>

UnrecognuzedDeposits is the drived class. HfkdInputTransactStorageEntity is the base entity. SumToSplit is the field we add to the base class.

can you help me?

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 13-Dec-2009 21:43:39   

As you are calculating this at runtime you don't need to add this to the fields collection. Just add it as a property, that is ready to databinding wink

David Elizondo | LLBLGen Support Team
rdhatch
User
Posts: 198
Joined: 03-Nov-2007
# Posted on: 13-Dec-2009 22:34:41   

I concur with David; he is right on.

Simply add a new property to a partial class of your Entity. You can databind directly to it. Using an EntityProperty object - you should also be able to filter/sort an EntityView/EntityCollection by it. EntityProperty objects can even be used in Projections.

We are doing this successfully. Hope this helps!

Ryan

mma02
User
Posts: 26
Joined: 04-Aug-2009
# Posted on: 14-Dec-2009 08:47:49   

Hi guys,

thanks for the ideas, we don't want to edit the llblgen's project that contains our entities because it is auto generated and we will have to remember to add this partial class every time the project is regenerated.

is there another way to go about it?

p.s i didnt add the new field to the fields collection, i have just inherited the entity class i want to extend and added a new property to it. my problem is at serialization when crossing service boundary.

thanks alot, Avi.

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 14-Dec-2009 09:20:32   

thanks for the ideas, we don't want to edit the llblgen's project that contains our entities because it is auto generated and we will have to remember to add this partial class every time the project is regenerated.

Only the generated files get overwritten when re-generating the code. So if you introduce a new file to the project it won't get overwritten, and that's where you should add the new property.

mma02
User
Posts: 26
Joined: 04-Aug-2009
# Posted on: 14-Dec-2009 09:39:43   

Only the generated files get overwritten when re-generating the code. So if you introduce a new file to the project it won't get overwritten, and that's where you should add the new property.

thats good, thanks walaa.

this will be our plan B :-)

can we handle this without changing the existing llblgen project?

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 14-Dec-2009 09:57:39   

we need to add a field to an entity at run time, the reasone is that we need to bind this entity to a grid and we need to display this extra column which is a summery of data that is available at runtime.

what we did is inherrite the entity we want to add the field to, added the property and added the field to the Fields collection.

Then your first solution is the way to go. Except the last line (in bold), you should not add the new property to the sub-Entity's Fields collection.

mma02
User
Posts: 26
Joined: 04-Aug-2009
# Posted on: 14-Dec-2009 10:12:40   

sorry for not being clear at my first message walaa, we did try adding the field to the Fields collection, this did not work and then i tryied again without adding the field to the collection and only adding a public property to the drived class, the problem i have described is still happening.

thanks.

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 14-Dec-2009 10:29:15   

So you have a WCF service and a client. And you have 2 methods in the WCF service. 1st one: Sends a collection of the sub-Entity to the client, and this one works. 2nd one: receives a collection of the sub-Entity, and this one doesn't work.

Am I right, so far?

Would you please post the Operation-Contracts of these 2 mthods?

mma02
User
Posts: 26
Joined: 04-Aug-2009
# Posted on: 14-Dec-2009 10:52:03   

yes you are right.

works: List<UnrecognizedDeposits> GetNotIdentifiedTransactions(InputTransactionFilterData inputTransactionFilterData);

dosen't work: ValidationResponse Save( IValidationContext Context, HfkdInputTransactStorageEntity currentEntity, HfkdInputTransactStorageEntity originalEntity, WorkingMode workingMode, ValidationData validationData);

the base entity is HfkdInputTransactStorageEntity and our sub entity is UnrecognizedDeposits.

at the Save method we cast the UnrecognizedDeposits to HfkdInputTransactStorageEntity \ and pass it as the parameter named originalEntity.

note that we do not use the DataContractSerializer but changed it to NetDataContractSerializer so we should be able to pass drived classes when the contract declares a base class - this works at other cases.

thanks.

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 14-Dec-2009 14:57:09   

Did you list the SubClass type as a ServiceKnownType?

mma02
User
Posts: 26
Joined: 04-Aug-2009
# Posted on: 14-Dec-2009 15:16:41   

Did you list the SubClass type as a ServiceKnownType

no, i didn't. but because we use the NetDataContractSerialyzer, the type information is serialized to, this is the regular dot net serialization not the default wcf serialization.

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 14-Dec-2009 15:30:46   

no, i didn't. but because we use the NetDataContractSerialyzer, the type information is serialized to, this is the regular dot net serialization not the default wcf serialization.

Would you please try that out?

Also, is the sub-Entity type known at the service side?

mma02
User
Posts: 26
Joined: 04-Aug-2009
# Posted on: 15-Dec-2009 06:20:23   

Would you please try that out?

Hi, i did that and the result is the same.

Also, is the sub-Entity type known at the service side?

yes it is.

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 15-Dec-2009 10:43:31   

Would you please attach a simple repro solution so we may debug it over here. For simplicity please make it against Northwind database.

Thanks.

mma02
User
Posts: 26
Joined: 04-Aug-2009
# Posted on: 15-Dec-2009 10:57:39   

so you say it should work if i only add a public property?

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 15-Dec-2009 11:02:31   

Yes. Also what you are trying should also work. If you want, you may send us a repro to test.

mma02
User
Posts: 26
Joined: 04-Aug-2009
# Posted on: 15-Dec-2009 11:09:26   

OK thank you, i will.

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 15-Dec-2009 11:49:46   

I'll temporarily close this thread. And it will be automatically re-opened once you post a reply.

Thanks.