c# - 通用存储库中的 Ef 核心多对多关系
问题描述
我正在尝试在我的 Web 应用程序中处理多对多关系。我正在使用通用存储库基本代码。
这是我的实体
public class UserEntity : BaseEntity<int>
{
public string EmployeeId { get; set; }
public string FullName { get; set; }
public string Email { get; set; }
public virtual ICollection<UserRoleEntity> UserRoles { get; set; }
}
public class RoleEntity : BaseEntity<int>
{
public string Name { get; set; }
public virtual ICollection<UserRoleEntity> Users { get; set; }
}
public class UserRoleEntity : BaseEntity<Guid>
{
public int UserId { get; set; }
public int RoleId { get; set; }
public virtual UserEntity UserEntity { get; set; }
public virtual RoleEntity RoleEntity { get; set; }
}
这是上下文模型配置
private void ConfigureUserRole(EntityTypeBuilder<UserRoleEntity> builder)
{
builder.ToTable("UserRole");
//there is no need for a surrogate key on many-to-many mapping table
builder.Ignore("Id");
builder.HasKey(ur => new { ur.RoleId, ur.UserId });
builder.HasOne(ur => ur.RoleEntity)
.WithMany(r => r.Users)
.HasForeignKey(ur => ur.RoleId);
builder.HasOne(ur => ur.UserEntity)
.WithMany(u => u.UserRoles)
.HasForeignKey(ur => ur.UserId);
}
这是我的通用存储库的主要 get 方法(实际上是Chris Pratt的实现)请注意这一query.Include
行,通用存储库的 get 方法使用 EF Core 的Include
api 来获取作为字符串参数的依赖实体。
protected virtual IQueryable<T> GetQueryable(Expression<Func<T, bool>> filter = null, Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null,
string includeProperties = null,
int? skip = null,
int? take = null)
{
includeProperties = includeProperties ?? string.Empty;
IQueryable<T> query = _dbContext.Set<T>();
if (filter != null)
{
query = query.Where(filter);
}
foreach (var includeProperty in includeProperties.Split
(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
{
query = query.Include(includeProperty);
}
if (orderBy != null)
{
query = orderBy(query);
}
if (skip.HasValue)
{
query = query.Skip(skip.Value);
}
if (take.HasValue)
{
query = query.Take(take.Value);
}
return query;
}
在我的服务中,我想获取用户数据及其全部依赖项。
public class UserService : IUserService
{
private IRepository<UserEntity> _userRepository;
private IRepository<RoleEntity> _roleRepository;
public UserService(IRepository<UserEntity> userRepository, IRepository<RoleEntity> roleRepository)
{
_userRepository = userRepository;
_roleRepository = roleRepository;
}
public UserEntity GetUserData(string employeeId)
{
var user = _userRepository.GetFirst(u => u.EmployeeId == employeeId, includeProperties: "UserRoles");
//var roles = _roleRepository.GetAll().ToList();
return user;
}
}
当执行上述服务代码时,我得到了用户及其UserRole
依赖项,但是,当我检查我需要UserRole
的RoleEntity
依赖项时,它是null
.
如何
RoleEntity
仅通过修改通用存储库的 get 方法来获取依赖数据?其次,服务代码 ( ) 中注释的行
_roleRepository.GetAll()
,当它也被执行时,RoleEntity
立即用它的正确值填充。情况如何?
解决方案
您的包含路径只有一个从UserEntity
to的跃点UserRoleEntity
(通过UserRoles
属性。您需要包含下一步以确保您还捕获RoleEntity
. 为此,请将路径更改为UserRoles.RoleEntity
,例如:
var user = _userRepository.GetFirst(
u => u.EmployeeId == employeeId,
includeProperties: "UserRoles.RoleEntity");
推荐阅读
- nginx - 我正在尝试使用 Nginx 配置 Flask 应用程序
- python - 空列:对象是 'float' 并且没有 len()
- ios - 如果我有 iPhone,是否可以在 Windows 上调试 Flutter/React Native IOS 应用程序?
- amazon-web-services - 如何通过 IGW 提供对 Internet 的子网访问?
- python - 通过在“;”处拆分值来计算字典列表中特定键的值计数器
- python - 计算缺失数据的数据框的平均值
- javascript - 如何通过 ID 获取用户
- r - 根据 r 中的行和将行值更改为 0
- amazon-web-services - DynamoDB 使用无服务器 Python 模板为正文提供 KeyError
- javascript - ReactJS:如何在循环中维护多个 ReadMore/ReadLess 按钮的索引