Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用于显式加载引用和集合的EF Core helper方法_C#_Entity Framework_Reflection_Lambda_Entity Framework Core - Fatal编程技术网

C# 用于显式加载引用和集合的EF Core helper方法

C# 用于显式加载引用和集合的EF Core helper方法,c#,entity-framework,reflection,lambda,entity-framework-core,C#,Entity Framework,Reflection,Lambda,Entity Framework Core,EF Core支持。上下文有两个重载,一个用于引用,一个用于集合 有两种方法是没有用的,而且会变得混乱。我想要一个单一的方法来接受两者作为params数组 所以不是这个 await context.Entry(customer).Collection(e => e.Orders).LoadAsync(); await context.Entry(customer).Collection(e => e.Returns).LoadAsync(); await context.Entry(

EF Core支持。上下文有两个重载,一个用于引用,一个用于集合

有两种方法是没有用的,而且会变得混乱。我想要一个单一的方法来接受两者作为params数组

所以不是这个

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();
    }
  }
}
公共异步任务加载(TEntity实体,参数表达式[]propertyExpressions)
地点:班级
{
foreach(propertyExpressions中的var propertyExpression){
var isCollection=typeof(IEnumerable).GetTypeInfo()
.IsAssignableFrom(propertyExpression.Body.Type);
if(isCollection)
{
等待进入(实体)
.Collection(propertyExpression)//问题就在这里!!!!!
.LoadAsync();
}
其他的
{
等待进入(实体)
.Reference(propertyExpression)
.LoadAsync();
}
}
}
问题行如上图所示。输入是
对象
,但
.Collection()
需要
IEnumerable


如何实现这一点?

考虑到这两个方法都返回派生类,并且都使用
Microsoft.EntityFrameworkCore.Internal.ExpressionExtensions.GetPropertyAccess
方法从传递的lambda表达式中获取
PropertyInfo.Name
,您可以使用相同的方法检索名称,然后使用以下方法:

使用Microsoft.EntityFrameworkCore.Internal

public async Task Load<TEntity>(TEntity entity, params Expression<Func<TEntity, object>>[] propertyExpressions)
    where TEntity : class
{
    foreach (var propertyExpression in propertyExpressions)
    {
        var propertyName = propertyExpression.GetPropertyAccess().Name;
        await Entry(entity).Navigation(propertyName).LoadAsync();
    }
}
公共异步任务加载(TEntity实体,参数表达式[]propertyExpressions)
地点:班级
{
foreach(propertyExpressions中的var propertyExpression)
{
var propertyName=propertyExpression.GetPropertyAccess().Name;
等待条目(实体).Navigation(propertyName).LoadAsync();
}
}

您可以使用
Collection()
的字符串重载并传递属性表达式的属性名。@CodeCaster是的,我看到了重载。。。如果可能的话,我想让它在没有魔术弦的情况下工作。我一直在使用这种方法,直到我能让它起作用为止。