使用Entity Framework Core中的多个导航属性加载数据

问题描述

我有以下实体:

public class BetSelection
    {
        public long Id { get; set; }
        public int BetId { get; set; }
        public virtual Bet Bet { get; set; }
        public long SelectionId { get; set; }
        public virtual Selection Selection { get; set; 
    }
public class Selection
    {
        public long Id { get; set; }
        public int SelectionTypeId { get; set; }
        public virtual SelectionType SelectionType { get; set; }
    }

public class SelectionType
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

并且我正在使用以下查询获取所需的BetSelection模型:

public async Task<BetSelection> GetAsync(int tableId,int roundId,string SelectionTypeName)
        {
            return await GetFirstOrDefaultAsync(bs => bs.Selection.GaMetableId == tableId && bs.Selection.RoundId == rounded && bs.Selection.SelectionType.Name == SelectionTypeName,includeProperties: $"{nameof(BetSelection.Selection)},{nameof(BetSelection.Selection.SelectionType)}");
        }

public async Task<T> GetFirstOrDefaultAsync(Expression<Func<T,bool>> filter = null,string includeProperties = null)
        {
            IQueryable<T> query = dbSet;

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

            if (includeProperties != null)
                foreach (var includeProperty in includeProperties.Split(new char[] { ',' },StringSplitOptions.RemoveEmptyEntries))
                    query = query.Include(includeProperty);

            return await query.FirstOrDefaultAsync();
        }

但是它抛出错误

"Invalid include path: 'SelectionType' - Couldn't find navigation for: 'SelectionType'_   at Microsoft.EntityFrameworkCore.Query.Internal.NavigationExpandingExpressionVisitor.ProcessInclude(NavigationExpansionExpression source,Expression expression,Boolean thenInclude)\r\n   at Microsoft.EntityFrameworkCore

看起来导航属性的使用方式错误。我想念什么吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)