[英]Ef core many to many relationship inside a generic repository
我正在尝试在我的 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
。
如何仅通过修改通用存储库的 get 方法来获取依赖的RoleEntity
数据?
其次,服务代码中注释的行( _roleRepository.GetAll()
),当它也被执行时, RoleEntity
会立即填充其正确的值。 它是如何发生的?
您的包含路径只有从UserEntity
到UserRoleEntity
的单跳(通过UserRoles
属性。您需要包含下一步以确保您也捕获RoleEntity
。为此,请将您的路径更改为UserRoles.RoleEntity
,例如:
var user = _userRepository.GetFirst(
u => u.EmployeeId == employeeId,
includeProperties: "UserRoles.RoleEntity");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.