【问题标题】:loading related entities from .NET Core EF using Code First使用 Code First 从 .NET Core EF 加载相关实体
【发布时间】:2017-04-10 14:28:37
【问题描述】:

我正在尝试使用 .NET Core EF 和 Code First 加载相关实体,但是我收到以下错误:-

System.InvalidOperationException: The property 'Roles' is not a navigation property of entity type 'ApplicationUser'. The 'Include(string)' method can only be used with a '.' separated list of navigation property names.

我的 2 个模型如下所示

public class Role
{
    public short Id { get; set; }
    public string Name { get; set; }

    public ICollection<ApplicationUser> Users { get; set; }
}

public class ApplicationUser
{
    public long Id { get; set; }

    [Required(ErrorMessage = "This field is required")]
    public string Name { get; set; }
    [Required(ErrorMessage = "This field is required")]
    public string Surname { get; set; }
    public string HomeNo { get; set; }
    public string MobNo { get; set; }
    public short RoleId { get; set; }

    public string UserName { get; set; }
    [RegularExpression(@"^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$", ErrorMessage = "Invalid Email Address")]
    public string Email { get; set; }
    public string Password { get; set; }

    public Role Role { get; set; }

我的上下文如下所示:-

        modelBuilder.Entity<Role>().ToTable("Role");

        modelBuilder.Entity<ApplicationUser>().HasOne(c => c.Role)
                                        .WithMany(r => r.Users)
                                        .HasForeignKey(c => c.RoleId);
        modelBuilder.Entity<ApplicationUser>().ToTable("ApplicationUser");

我正在尝试通过以下方法获取已验证用户:-

   var verifiedUser = await GetById<ApplicationUser>(m => m.Email == user.Email, "Roles");

依次调用 GenericService :-

    public async Task<T> GetById<TKey>(
        Expression<Func<T, bool>> filter = null,
        string includeProperties = "")
    {
        return await _genericRepository.Get<T>(filter, includeProperties);
    }

最后是 GenericRepository:-

    public async Task<T> Get<TKey>(Expression<Func<T, bool>> filter = null, string includeProperties = "")

    {
        IQueryable<T> query = Context.Set<T>();
        query = IncludePropertiesQuery(query, includeProperties);

        if (filter != null)
        {
            query = query.Where(filter);
        }

        return await query.SingleOrDefaultAsync();
    }

IncludePropertiesQuery 如下所示:-

    private IQueryable<T> IncludePropertiesQuery(IQueryable<T> query, string includeProperties = "")
    {
        if (includeProperties == null)
        {
            includeProperties = "";
        }

        includeProperties = includeProperties.Trim() ?? string.Empty;
        foreach (var includeProperty in includeProperties.Split
            (new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
        {
            query = query.Include(includeProperty);
        }

        return query;

    }

这与我的 DbContext 以及我在 2 个表之间的关系有关吗?

感谢您的帮助和时间

【问题讨论】:

  • 你有没有得到答案,我也有同样的问题

标签: c# asp.net-core ef-code-first .net-core ef-code-first-mapping


【解决方案1】:

如果您的“includeProperties”参数为 null 或为空,则不能使用“Include”扩展方法。为您的代码添加逻辑以解决此问题。

【讨论】:

  • 当它为 null 或为空(字符串 includeProperties = ""))时它确实有效。问题是当我在这种情况下包含类似“角色”的内容时。
  • 用 IncludePropertiesQuery 更新了问题
  • 我也更新了我得到的错误,现在可能更清楚了
【解决方案2】:

显然 EF7 中的自动延迟加载存在问题

see problem here

所以我在获取用户后做了一个额外的步骤来获取实际角色

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-02
    • 2023-03-12
    • 2018-01-15
    • 2011-11-04
    • 1970-01-01
    • 1970-01-01
    • 2016-08-01
    • 1970-01-01
    相关资源
    最近更新 更多