1

I am trying to load related entities using .NET Core EF and Code First, however I am getting the following error :-

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.

My 2 models look as follows

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

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

and

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

my Context looks as follows :-

        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");

and I am trying to get the verifiedUser in a method as follows:-

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

which in turn calls a GenericService :-

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

and finally the 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();
    }

The IncludePropertiesQuery looks like this :-

    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;

    }

Is it something to do with my DbContext and the relationship I have between the 2 tables?

Thanks for your help and time

JMon
  • 3,387
  • 16
  • 63
  • 102

2 Answers2

0

If your "includeProperties" parameter is null or empty, you cannot use the "Include" extension method. Add logic to your code to work around this.

Ricardo Peres
  • 13,724
  • 5
  • 57
  • 74
0

Apparently there is an issue with automatic lazy loading in EF7

see problem here

So I did an extra step to get the actual role after I get the user

JMon
  • 3,387
  • 16
  • 63
  • 102