3

As part of a generic repository pattern I have a DbSet<TEntity> - a generic Entity Framework IQueryable collection. The type of TEntity could vary, but I know I want to sort by whatever the primary key is.

Can I do this generically in EF, without knowing the type in advance? Otherwise how can we do Skip() -> Take() for repeaters without knowing the type?

haim770
  • 48,394
  • 7
  • 105
  • 133
SuperNES
  • 2,760
  • 9
  • 37
  • 49

1 Answers1

2

These extension methods assumes that a key field in your entity class is properly decorated with the Key attribute.

Community
  • 1
  • 1
Sievajet
  • 3,443
  • 2
  • 18
  • 22
  • Using the `[Key]` attribute is only one way to indicate the primary-key property. The most reliable way is to query the context metadata to determine the primary-key. – haim770 Dec 24 '14 at 11:15
  • Good answer. I think I'm going to make my own [DefaultSort] attribute and use your extension method to find it (or throw an exception if it isn't there). – SuperNES Dec 26 '14 at 15:16