Unwanted download with UserManager
So, I created this User Service, which inherits from UserManager and looks like this:
/// <summary>
/// Service for handling users
/// </summary>
public class UserService : UserManager<User>
{
/// <summary>
/// Default constructor
/// </summary>
/// <param name="store">The user repository</param>
public UserService(IUserStore<User> store)
: base(store)
{
// Allow the user service to use email instead of usernames
this.UserValidator = new UserValidator<User>(this)
{
AllowOnlyAlphanumericUserNames = false
};
}
/// <summary>
/// A static method that creates a new instance of the user service
/// </summary>
/// <param name="options">Any options that should be supplied</param>
/// <param name="context">The Owin context</param>
/// <returns>The user service</returns>
public static UserService Create(IdentityFactoryOptions<UserService> options, IOwinContext context)
{
// Get our current database context
var dbContext = context.Get<DatabaseContext>();
// Create our service
var service = new UserService(new UserStore<User>(dbContext));
// Allow the user service to use email instead of usernames
service.UserValidator = new UserValidator<User>(service)
{
AllowOnlyAlphanumericUserNames = false
};
// Assign our email service to our user service
service.EmailService = new EmailService();
// Get our data protection provider
var dataProtectionProvider = options.DataProtectionProvider;
// If our data protection provider is not nothing
if (dataProtectionProvider != null)
{
// Set our token provider
service.UserTokenProvider = new DataProtectorTokenProvider<User>(dataProtectionProvider.Create("ASP.NET Identity"))
{
// Code for email confirmation and reset password life time
TokenLifespan = TimeSpan.FromHours(6)
};
}
// Return our service
return service;
}
}
But I disabled LazyLoading in the DbContext . So now I have a problem. The User may have Hubs , but they are mostly owned by the Company , so a lookup table is created which I mapped in the DbContext like this:
// Create lookup tables
modelBuilder.Entity<Center>()
.HasMany(m => m.Users)
.WithMany(m => m.Centers)
.Map(m =>
{
m.MapLeftKey("CenterId");
m.MapRightKey("UserId");
m.ToTable("UserCenters");
});
So now I need to access the User Centers , but the Identity Framework doesn't seem to support Eager Loading. Has anyone found this a problem before and does anyone know how I can use EagerLoading with UserManager?
Cheers, / R 3plica
+3
source to share
1 answer
Damn it was easy to solve. The UserManager actually exposes the DbSet to users as an IQueryable, so you can actually add Include there, so I just created this function:
/// <summary>
/// Gets all users
/// </summary>
/// <param name="includes">Optional parameter for eager loading related entities</param>
/// <returns>An list of users</returns>
public IQueryable<User> GetAll(params string[] includes) {
// Get our User DbSet
var users = base.Users;
// For each include, include in the query
foreach (var include in includes)
users = users.Include(include);
// Return our result
return users;
}
and then in my controllers I did this:
/// <summary>
/// Gets the centers assigned to a user
/// </summary>
/// <param name="userId">The id of the user</param>
/// <returns>All centers for the user</returns>
[HttpGet]
[Route("", Name = "GetCentersByUser")]
public IHttpActionResult Get(string userId)
{
// Get our user
var user = this.UserService.GetAll("Centers").Where(m => m.Id.Equals(userId, StringComparison.OrdinalIgnoreCase)).SingleOrDefault();
// If the user doesn't exist, throw an error
if (user == null)
return BadRequest("Could not find the user.");
// Return our centers
return Ok(user.Centers.Select(m => this.ModelFactory.Create(m)));
}
+3
source to share