We used to store our Enums in a database table that had a Code
property that corresponded to Enums in the application. The point of this was that we could give the Enums in the database a friendly name which allowed us to access it easily when needed.
Recently we stopped having an Enum table in the database and used the Description attribute on each Enum and used reflection to get the Description as the friendly name. This was great as it meant we had less tables in the database.
Now I've come across the problem when performing a Linq Select
statement on the DatabaseContext (I need to keep it as an IQueryable
) that we can't use the extension method on the Enum to get the friendly name as Entity Framework wont recognise methods.
The current code is shown below. The ItemPriority
is assigned the Enumn description attribute which uses reflection. This code falls over as EF can't recognise the method.
return await OrderItems(items).Skip(pageIndex * pageSize).Take(pageSize)
.Select(item => new ItemViewModel
{
Id = item.Id,
Description = item.Description,
ItemPriority = item.Priority.Description(),
}).ToListAsync();
Is there another way I can apply friendly names to Enums or is having the friendly names in the database the only way to go? If I used the database I could just do the following:
return await OrderItems(items).Skip(pageIndex * pageSize).Take(pageSize)
.Select(item => new ItemViewModel
{
Id = item.Id,
Description = item.Description,
ItemPriority = item.Priority.Name,
}).ToListAsync();
Aucun commentaire:
Enregistrer un commentaire