Using EFCore with Authentication for the Kernel

Over the past few days, I have been battling the Identity system in conjunction with the Entity Framework Core. Here is some information before identifying my problem:

  • The Framework Core object does not support lazy loading, which means I use eager loading, telling which elements to include
  • To indicate which elements of the object I would like to receive, I redefine the method in each repository (the properties that were restored are permanent)
  • The Identity system that I use 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 about the problem itself:

I would like to use an identification system to generate tokens, receive users, etc., but I cannot load the UserManager object UserManager , since it returns the Task<ApplicationUser> methods, and the Include() method itself requires IQueryable (in order to load) . What is the general way to be able to use impatient loading and UserManager ?

+7
asp.net-core entity-framework asp.net-identity
source share
2 answers

The easiest way is to directly call UserManger.Users. Like this:

 ApplicationUser user = await userManager.Users.Include(s => s.Sales).Where(e => e.Email == " t@t.net ").FirstOrDefaultAsync(); 
+2
source share

Perhaps you want to use bootstrap and UserManager as follows:

  private async Task<ApplicationUser> GetSingleUser(Expression<Func<ApplicationUser, bool>> predicate, params Expression<Func<ApplicationUser, object>>[] includeProperties) { IQueryable<ApplicationUser> query = _userManager.Users; foreach (var includeProperty in includeProperties) { query = query.Include(includeProperty); } return await query.Where(predicate).FirstOrDefaultAsync(); } 

Hope this help!

0
source share

All Articles