Guest User

Untitled

a guest
Jul 16th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. public class MyDbContext : IdentityDbContext<...>
  2. {
  3. public DbSet<EntityA> EntitiesA { get; set; }
  4. public DbSet<EntityB> EntitiesB { get; set; }
  5. }
  6.  
  7. public static class EntitiesA
  8. {
  9. private static Func<MyDbContext, ...> _queryEntitiesA = EF.CompileAsyncQuery((MyDbContext context, ...) => context...);
  10.  
  11. public static async Task<EntityA> QueryEntitiesA(this DbSet<EntityA> dbSet, ...)
  12. {
  13. var context = dbSet.GetContext();
  14. return await _queryEntitiesA(...)
  15. }
  16. }
  17.  
  18. public static class Common
  19. {
  20. private static Dictionary<Type, FieldInfo> _fields = new Dictionary<Type, FieldInfo>();
  21. public static MyDbContext GetContext<TEntity>(this DbSet<TEntity> dbSet) where TEntity : class
  22. {
  23. var type = typeof(TEntity);
  24. if (!Common._fields.ContainsKey(type))
  25. {
  26. Common._fields.Add(type, typeof(InternalDbSet<TEntity>).GetField("_context", BindingFlags.NonPublic | BindingFlags.Instance));
  27. }
  28. return Common._fields[type].GetValue(dbSet) as MyDbContext;
  29. }
  30. }
Add Comment
Please, Sign In to add comment