Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/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# efcore-具有导航属性的投影_C#_Entity Framework_Asp.net Core_Entity Framework Core - Fatal编程技术网

C# efcore-具有导航属性的投影

C# efcore-具有导航属性的投影,c#,entity-framework,asp.net-core,entity-framework-core,C#,Entity Framework,Asp.net Core,Entity Framework Core,从IdentityRole(ApplicationRole)表获取角色时,我会得到以下输出: [ { "description": "developer role", "tenantId": "426b22f1-8baf-41e2-bce6-53e5c6b45974", "createdAt": "2017-02-07T11:56:46", "modifiedAt": null, "users": [], "claims": [ {

从IdentityRole(ApplicationRole)表获取角色时,我会得到以下输出:

[
  {
    "description": "developer role",
    "tenantId": "426b22f1-8baf-41e2-bce6-53e5c6b45974",
    "createdAt": "2017-02-07T11:56:46",
    "modifiedAt": null,
    "users": [],
    "claims": [
      {
        "id": 1,
        "roleId": "59961dcc-6eb6-4509-9355-cca9f61199f3",
        "claimType": "http://schemas.microsoft.com/identity/claims/permission",
        "claimValue": "user:read"
      },
      {
        "id": 2,
        "roleId": "59961dcc-6eb6-4509-9355-cca9f61199f3",
        "claimType": "http://schemas.microsoft.com/identity/claims/permission",
        "claimValue": "user:delete"
      }
    ],
    "id": "59961dcc-6eb6-4509-9355-cca9f61199f3",
    "name": "developer426b22f1-8baf-41e2-bce6-53e5c6b45974",
    "normalizedName": "DEVELOPER426B22F1-8BAF-41E2-BCE6-53E5C6B45974",
    "concurrencyStamp": "4fca1eb6-7ff2-45f9-99d9-2e1a2f85c562"
  }
]
我需要的输出是:

[
  {
    "name": "developer426b22f1-8baf-41e2-bce6-53e5c6b45974",
    "description": "developer role",
    "claims": ["user:read","user:delete"],
    "id": "59961dcc-6eb6-4509-9355-cca9f61199f3",
    "createdAt": "0001-01-01T00:00:00",
    "modifiedAt": null
  }
]
因此,我尝试使用Dto,并在Select()中传递它的属性:

公共类应用程序角色to:BaseDto
{
公共字符串名称{get;set;}
公共字符串说明{get;set;}
公共列表声明{get;set;}
公共静态表达式SelectProperties=(角色)=>new ApplicationRoleTo
{
Id=role.Id,
Name=role.Name,
Description=role.Description,
Claims=GetClaimsList(role.Claims),
CreatedAt=role.CreatedAt,
ModifiedAt=role.ModifiedAt
};
私有静态列表GetClaimsList(ICollection角色列表)
{
var索赔=新列表();
foreach(roleClaims中的var roleClaim)
{
索赔。添加(roleClaim.ClaimValue);
}
退货索赔;
}
}
当我使用Select()投影时

GetQueryable<TEntity>(null, orderBy, includeProperties, skip, limit).Select(selectProperties).ToListAsync();
GetQueryable(null,orderBy,includeProperties,skip,limit)。选择(selectProperties)。toListSync();
我发现以下错误:

System.InvalidOperationException: The type of navigation property 'Claims' on the entity type 'IdentityRole<string, IdentityUserRole<string>, Identi
tyRoleClaim<string>>' is 'ICollection<IdentityRoleClaim<string>>' for which it was not possible to create a concrete instance. Either initialize the prope
rty before use, add a public parameterless constructor to the type, or use a type which can be assigned a HashSet<> or List<>.
System.InvalidOperationException:实体类型“IdentityRole”上导航属性“Claims”的类型为“ICollection”,无法为其创建具体实例。或者初始化属性
在使用之前,请向该类型添加公共无参数构造函数,或使用可分配哈希集或列表的类型。

您是否尝试使用另一个
Select
而不是
foreach
来获取您的声明?@Mats391我在Include()中传递声明,因此它应该在结果中可用,为什么在您使用
Select
时我要使用另一个Select(),EF应该能够将其转换为正确的SQL。那么您就不需要
包含
。但是,对于Include,它也应该可以工作,所以只是在黑暗中拍摄。您是否尝试使用另一个
Select
来获取您的声明,而不是
foreach
?@Mats391我在Include()中传递声明,因此它应该在结果中可用,为什么我要使用另一个Select()当您使用
Select
EF时,它应该能够将其转换为正确的SQL。那么您就不需要
包含
。然而,对于Include,它也应该可以工作,所以只需在黑暗中拍摄即可。
System.InvalidOperationException: The type of navigation property 'Claims' on the entity type 'IdentityRole<string, IdentityUserRole<string>, Identi
tyRoleClaim<string>>' is 'ICollection<IdentityRoleClaim<string>>' for which it was not possible to create a concrete instance. Either initialize the prope
rty before use, add a public parameterless constructor to the type, or use a type which can be assigned a HashSet<> or List<>.