Inheriting Entity Framework: sort / group by type?

The entity structure (and the RIA services I use it with) support inheritance well. The database mapping can be single-table or multi-column, and in the first case, the database table contains a pointer column containing the type designation.

Obviously, this pointer does not appear in the model, even if this is what you would need for ordering and grouping.

Should I introduce an additional explicit pointer type in the base class if this is what I want to do, or is there a better way to sort or group by type in some way?

I am using EF5.

+4
source share
1 answer

Yes, I can provide scenarios where you need a list of basic types. Unfortunately, there is no way to get discriminator (designation) in the conceptual model. The only way I can think of is something like

db.BaseObjects.Where(b => ...) .AsEnumerable().Select (b => new {b.GetType().Name, b } ) 

After that you can sort / group by type name. But you cannot project ( Select ) to AsEnumerable , so the only way to limit the size of the data is by filtering ( Where ).

There is a trick to getting the discriminator column as a visible property in the class model. You can create a computed column in the table that simply displays the column value of the real discriminator and matches it with the property that is marked as DatabaseGeneratedOption.Computed .

Please note that this requires special attention with the code first (migration). And this requires some performance with inserts and updates, because EF subsequently needs to read the value from the database.

+2
source

All Articles