Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using EFCore with Identity for Core

For the last few days I've been fighting the Identity system in combination with Entity framework Core. Here's some information prior to revealing my problem:

  1. Entity framework Core doesn't support lazy loading, this means that I'm using eager loading by saying which items to include
  2. In order to say which items of an object I would like to retrieve I override a method in each repository ( the properties retrieved are constant for now )
  3. The Identity system I'm using is configured as :

    services.AddIdentity<ApplicationUser, ApplicationRole>(options =>
    {
        options.Cookies.ApplicationCookie.AutomaticChallenge = false;
    })  .AddUserManager<UserManager>()
        .AddRoleManager<RoleManager>()
        .AddUserStore<UserStore>()
        .AddRoleStore<RoleStore>()
        .AddEntityFrameworkStores<ApplicationDbContext, int>()
        .AddDefaultTokenProviders();
    

Now on to the problem itself:

I would like to use the identity system in order to generate tokens, get users etc. but I cannot do eager loading on the UserManager object itself as it's methods return Task<ApplicationUser> and the Include() method itself requires IQueryable ( to do eager loading ). What is the general way to be both be able to use eager loading and UserManager?

like image 851
Christo S. Christov Avatar asked Jun 04 '17 14:06

Christo S. Christov


1 Answers

The simplest way maybe to call UserManger.Users directly. Like this:

ApplicationUser user = await userManager.Users.Include(s => s.Sales).Where(e => e.Email == "[email protected]").FirstOrDefaultAsync();
like image 51
VHao Avatar answered Nov 15 '22 08:11

VHao