1
EF Core支持explicit loading。上下文有兩個重載,一個用於引用,另一個用於集合。EF核心助手方法用於顯式加載參考和集合
有兩種方法是沒有用的,並且變得混亂。我想要一個接受兩個參數數組的方法。
所以不是這個
await context.Entry(customer).Collection(e => e.Orders).LoadAsync();
await context.Entry(customer).Collection(e => e.Returns).LoadAsync();
await context.Entry(customer).Reference(e => e.Account).LoadAsync();
我想這樣做:
await context.Entry(customer).Load(e=>e.Orders, e=>e.Returns, e=>e.Account);
我認爲這是可能的,因爲沒有與context.Include(...)
類似的東西,它接受兼具收藏和參考。
在我的上下文類,我有這個至今:
public async Task Load<TEntity>(TEntity entity, params Expression<Func<TEntity, object>>[] propertyExpressions)
where TEntity : class
{
foreach (var propertyExpression in propertyExpressions) {
var isCollection = typeof(IEnumerable).GetTypeInfo()
.IsAssignableFrom(propertyExpression.Body.Type);
if(isCollection)
{
await Entry(entity)
.Collection(propertyExpression) // problem is here !!!!!
.LoadAsync();
}
else
{
await Entry(entity)
.Reference(propertyExpression)
.LoadAsync();
}
}
}
問題線如上圖所示。輸入爲object
,但.Collection()
預計爲IEnumerable<TProperty>
。
我該如何做這項工作?
您可以使用'Collection()'的字符串重載並傳遞屬性表達式的屬性名稱。 – CodeCaster
@CodeCaster是的,我看到了重載......儘管如果可能的話,我想讓它在沒有魔術字符串的情況下工作。我正在使用這種方法,直到我可以使這個工作。 – grokky