微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决使用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 举报,一经查实,本站将立刻删除。