score:2

Accepted answer

You can send projection data to BreezeJS and turn them into a custom EntityType you've defined on the client. You can't expect that ContactPerson type to show up in metadata from the server that has been generated by Entity Framework; it's a DTO and not part of the model that EF knows about.

That shouldn't stop you from defining ContactPerson on your Breeze client. Learn about how to create client-side metadata in the docs.

It's worth noting that you do not have to define all of your metadata on the client to take advantage of this feature. Just add this ContactPerson type.

The next trick is getting BreezeJS to realize that your projection data should be transformed into ContactPerson entity on the client.

Breeze won't recognize the anonymous type data by default. You can give it some help with the toType query clause. You can uncomment that clause in your query after you have defined the type in your client metadata.

return EntityQuery.from("PersonsFlattened")
       .toType("ContactPerson") // Should work after defining ContactPerson on client
       .orderBy(orderBy)
       .using(self.manager).execute()
       .then(querySucceeded, self._queryFailed);

You won't need the toType clause if you project into a server-side ContactPerson type and connect the dots between the "PersonsFlattened" endpoint and your custom client-side ContactPerson type in metadata. I think I'd rename that endpoint "ContactPersons" for consistency.

N.B.: I trust you realize that you've defined read-only types. Breeze doesn't know that so if you make changes to a ContactPerson entity in BreezeJS, the manager will try to save it. Your save attempt will throw on the server unless you catch the incoming change on and do something marvelous with it, perhaps in a BeforeSaveEntity method.

score:0

The exception "The entity or complex type 'SiteTrackerModel.ContactPerson' cannot be constructed in a LINQ to Entities query" is related to how the class ContactPerson is declared. LINQ to entity can only construct pur Data Transfert Object : class containing only public properties with trivial getter and setter and without constructor.

Check the ContactPerson class definition.


Related Articles