gpt4 book ai didi

c# - 使用 UserManager 预先加载

转载 作者:行者123 更新时间:2023-11-30 16:07:25 24 4
gpt4 key购买 nike

所以我创建了这个继承自 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。所以,现在我有一个问题。用户可以拥有 Centers,但它们主要属于 Company 因此创建了一个查找表,我已将其映射到我的 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 似乎不支持预加载。之前有没有人发现这是一个问题,有没有人知道我如何将 EagerLoading 与 UserManager 一起使用?

干杯,/r3副本

最佳答案

该死,这很容易解决。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;
}

然后在我的 Controller 中,我这样做了:

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

关于c# - 使用 UserManager 预先加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30832802/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com