使用UserManager急切加载

时间:2015-06-14 18:09:01

标签: c# entity-framework

所以我创建了这个继承自 UserManager 的用户服务,它看起来像这样:

/// <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;
    }
}

但我已在 DbContext 中停用 LazyLoading 。 所以,现在我有一个问题。用户可以拥有中心,但他们主要属于公司,因此会创建一个我在 DbContext 中映射的查找表,如下所示:

// 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");
    });

因此,现在我需要访问用户的中心,但似乎Identity Framework不支持Eager Loading。 有没有人发现这是一个问题,有谁知道如何使用EagerLoading与UserManager?

干杯, / r3plica

1 个答案:

答案 0 :(得分:3)

该死,这很容易解决。 UserManager实际上将Users DbSet公开为IQueryable,因此您可以实际添加Include,所以我只是创建了这个功能:

/// <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;
}

然后在我的控制器中,我这样做了:

/// <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)));
}