扩展方法中 EF Core ThenInclude 的 lambda 表达式中的非识别成员
Non-recognized member in lambda expression of EF Core ThenInclude in Extension method
我正在尝试编写一个扩展方法来包含我的许多实体模型中存在的某个 属性(文本元素,它们本身包含翻译集合)。
我对 .Include
函数没问题:
public static IIncludableQueryable<T, IEnumerable<Translation>> IncludeTextBitWithTranslations<T>(this IQueryable<T> source, Expression<Func<T, TextBit>> predicate) where T: class
{
var result = source.Include(predicate).ThenInclude(t => t.Translations);
return result;
}
测试成功。
现在,在某些情况下,我的实体的所有文本都在一个子实体中 - 例如,Article
实体有一个 ArticleInfo
属性,其中包含一些文本元素。所以我认为我只需要做另一个 ThenInclude
的扩展。有一些不同,我终于明白了:
public static IIncludableQueryable<TEntity, ICollection<Translation>> ThenIncludeTextBitWithTranslations<TEntity, TPreviousProperty, TextBit>(this IIncludableQueryable<TEntity, TPreviousProperty> source, Expression<Func<TPreviousProperty, TextBit>> predicate) where TEntity: class
{
var result = source.ThenInclude(predicate)
.ThenInclude(t => t.Translations);
return result;
}
现在我得到这个错误:
'TextBit' does not contain a definition for 'Translations' and no extension method 'Translations' accepting an argument of 'TextBit' type was found
这个错误出现在最后一个 lambda 表达式 t => t.Translations
.
这个错误对我来说非常奇怪,我一直在网上寻找有关此事的帮助,但我没有成功。
我尝试通过手动添加将类型强制为 ThenInclude
:
var result = source.ThenInclude(predicate)
.ThenInclude<TEntity, TextBit, ICollection<Translation>>(t => t.Translations);
但没有成功。
有人知道为什么吗?
这里我很茫然
您在第二个 (ThenIncludeTextBitWithTranslations<TEntity, TPreviousProperty, TextBit>
) 中有额外的类型参数 TextBit
,因此它被视为通用类型,而不是实际类型,请将其删除:
public static IIncludableQueryable<TEntity, ICollection<Translation>> ThenIncludeTextBitWithTranslations<TEntity, TPreviousProperty>(this IIncludableQueryable<TEntity, TPreviousProperty> source, Expression<Func<TPreviousProperty, TextBit>> predicate) where TEntity: class
{
var result = source.ThenInclude(predicate).ThenInclude(t => t.Translations);
return result;
}
我正在尝试编写一个扩展方法来包含我的许多实体模型中存在的某个 属性(文本元素,它们本身包含翻译集合)。
我对 .Include
函数没问题:
public static IIncludableQueryable<T, IEnumerable<Translation>> IncludeTextBitWithTranslations<T>(this IQueryable<T> source, Expression<Func<T, TextBit>> predicate) where T: class
{
var result = source.Include(predicate).ThenInclude(t => t.Translations);
return result;
}
测试成功。
现在,在某些情况下,我的实体的所有文本都在一个子实体中 - 例如,Article
实体有一个 ArticleInfo
属性,其中包含一些文本元素。所以我认为我只需要做另一个 ThenInclude
的扩展。有一些不同,我终于明白了:
public static IIncludableQueryable<TEntity, ICollection<Translation>> ThenIncludeTextBitWithTranslations<TEntity, TPreviousProperty, TextBit>(this IIncludableQueryable<TEntity, TPreviousProperty> source, Expression<Func<TPreviousProperty, TextBit>> predicate) where TEntity: class
{
var result = source.ThenInclude(predicate)
.ThenInclude(t => t.Translations);
return result;
}
现在我得到这个错误:
'TextBit' does not contain a definition for 'Translations' and no extension method 'Translations' accepting an argument of 'TextBit' type was found
这个错误出现在最后一个 lambda 表达式 t => t.Translations
.
这个错误对我来说非常奇怪,我一直在网上寻找有关此事的帮助,但我没有成功。
我尝试通过手动添加将类型强制为 ThenInclude
:
var result = source.ThenInclude(predicate)
.ThenInclude<TEntity, TextBit, ICollection<Translation>>(t => t.Translations);
但没有成功。
有人知道为什么吗?
这里我很茫然
您在第二个 (ThenIncludeTextBitWithTranslations<TEntity, TPreviousProperty, TextBit>
) 中有额外的类型参数 TextBit
,因此它被视为通用类型,而不是实际类型,请将其删除:
public static IIncludableQueryable<TEntity, ICollection<Translation>> ThenIncludeTextBitWithTranslations<TEntity, TPreviousProperty>(this IIncludableQueryable<TEntity, TPreviousProperty> source, Expression<Func<TPreviousProperty, TextBit>> predicate) where TEntity: class
{
var result = source.ThenInclude(predicate).ThenInclude(t => t.Translations);
return result;
}