EF核心在通用存储库中的多对多关系

时间:2018-10-31 14:09:55

标签: c# entity-framework-core

我正在尝试在我的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 Prattimplementation)请注意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的{​​{1}}依赖项时,它就是{{1 }}。

  • 如何仅通过修改通用存储库的get方法来获取依赖的RoleEntity数据?

  • 第二,在服务代码(null)中注释的行也被执行时,RoleEntity立即被填充为其适当的值。怎么回事?

1 个答案:

答案 0 :(得分:1)

您的包含路径只有一个从UserEntityUserRoleEntity的跃点(通过UserRoles属性。您需要包括下一步以确保您也捕获了{{1} }。为此,请将路径更改为RoleEntity,例如:

UserRoles.RoleEntity