使用Code First

时间:2017-04-10 14:28:38

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

我正在尝试使用.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");

我试图在一个方法中获得VerifiedUser如下: -

   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 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

see problem here

所以我在获得用户

之后做了一个额外的步骤来获得实际的角色