We've ran into a scenario where we need to be able to return the name of an entity type as a value in the result of an esql statement.
The problem is this, we have a generic helper in place that builds and runs an esql statement to return a result that's passed into a SelectList on an MVC3 application.
The text column is an expression taken from an attribute added to a property on the model.
So for example if we wanted the dropdown to list a persons name as Last, First we add an attribute with an expression like: 'it.[LastName] + ", " + it.[FirstName]' Which produces a statement like this:
SELECT it.[Id] AS Id, it.[LastName] + ", " + it.[FirstName] AS Name
FROM OfType([AuditedObject], [User]) AS it
We've run into a scenario involving an abstract type in EF4.3 using TPT. We need to enumerate the derived entities using this helper method. The method enumerates and returns all the derived entities fine when we just want basic data like this:
SELECT it.[Id] AS Id, it.[Name] AS Name
FROM OfType([AuditedObject], [DatumObject]) AS it
But we need a way to indicate what type each entity is. Something like this pseudo code:
SELECT it.[Id] AS Id, it.GetType().Name + " - " + it.[Name] AS Name
FROM OfType([AuditedObject], [DatumObject]) AS it
Does anyone have any knowledge of how I might accomplish this?
I forgot to swing back around and update this questions before. The solution we ended up going with wasn't very elegant but it gave us all the functionality we needed.
We ended up adding an entity to the model to represent the type of the root object with unidirectional association linking them. We then used a t4 template to generate a script to populate the types into the new table and added business logic to ensure it was set whenever a SaveChanges() was called.
Now we can do this:
SELECT it.[Id] AS Id, it.[EntityType].[Name] + " - " + it.[Name] AS Name
FROM OfType([AuditedObject], [DatumObject]) AS it