C# “如何修复实体框架核心”;参数类型不匹配";使用Linq选择投影

C# “如何修复实体框架核心”;参数类型不匹配";使用Linq选择投影,c#,linq,asp.net-core,.net-core,entity-framework-core,C#,Linq,Asp.net Core,.net Core,Entity Framework Core,当尝试使用实体框架核心和Linq进行直接投影时,我得到一个“参数类型不匹配”异常 我已经研究了可能的原因,并将其缩小到导致错误的Select(见下文)。有一个例子描述了类似的情况,其中有简单的类型和可选的导航实体,但没有一个建议的解决方案适合我。它不是一个nullable类型,我已经尝试在任何子属性上强制转换或使用Value。我还尝试在DbContext中将关系设置为required,但这并不理想 以下是存储库中的Linq查询: return await _dashboardContext.Pr

当尝试使用实体框架核心和Linq进行直接投影时,我得到一个“参数类型不匹配”异常

我已经研究了可能的原因,并将其缩小到导致错误的
Select
(见下文)。有一个例子描述了类似的情况,其中有简单的类型和可选的导航实体,但没有一个建议的解决方案适合我。它不是一个
nullable
类型,我已经尝试在任何子属性上强制转换或使用
Value
。我还尝试在
DbContext
中将关系设置为required,但这并不理想

以下是存储库中的Linq查询:

return await _dashboardContext.PresetDashboardConfig
    .Where(config => config.DashboardTypeId == dashboardType && config.OrganisationType = organisationType)
    .GroupBy(config => config.GroupId)
    .Select(config => new DashboardConfigDTO
    {
        DashboardType = config.First().DashboardTypeId,
        OrganisationId = organisationId,
        WidgetGroups = config.Select(group => new WidgetGroupDTO
        {
            Id = group.Id,
            Name = group.GroupName,
            TabOrder = group.TabOrder,

            // Problem Select below:
            Widgets = group.Widgets.Select(widget => new WidgetConfigDTO
            {
                IndicatorId = widget.IndicatorId,
                ScopeId = widget.ScopeId.ToString(),
                ParentScopeId = widget.ParentScopeId.ToString(),
                WidgetType = widget.WidgetType,
                WidgetSize = widget.WidgetSize,
                Order = widget.Order
            })
        })
    })
    .SingleOrDefaultAsync();
各实体:

public class DashboardConfig
{
    public int Id { get; set; }
    public int DashboardTypeId { get; set; }
    public int OrganisationType {get; set; }
    public int GroupId { get; set; }
    public string GroupName { get; set; }
    public int TabOrder { get; set; }
}

public class PresetDashboardConfig : DashboardConfig
{
    public ICollection<PresetWidgetConfig> Widgets { get; set; }
}

public class WidgetConfig
{
    public int Id { get; set; }
    public int IndicatorId { get; set; }
    public long ScopeId { get; set; }
    public long? ParentScopeId { get; set; }
    public int WidgetType { get; set; }
    public int WidgetSize { get; set; }
    public int Order { get; set; }
}

public class PresetWidgetConfig : WidgetConfig
{
    public int PresetDashboardConfigId { get; set; }
}
public类仪表板配置
{
公共int Id{get;set;}
public int DashboardTypeId{get;set;}
公共int组织类型{get;set;}
public int GroupId{get;set;}
公共字符串组名{get;set;}
public int TabOrder{get;set;}
}
公共类预置DashboardConfig:DashboardConfig
{
公共ICollection小部件{get;set;}
}
公共类WidgetConfig
{
公共int Id{get;set;}
公共int指示符{get;set;}
公共长作用域ID{get;set;}
public long?ParentScopeId{get;set;}
公共int WidgetType{get;set;}
public int WidgetSize{get;set;}
公共整数顺序{get;set;}
}
公共类预设WidgetConfig:WidgetConfig
{
public int PresetDashboardConfigId{get;set;}
}
最后,DbContext ModelBuilder:

modelBuilder.Entity<PresetDashboardConfig>(entity =>
{
    entity.Property(e => e.GroupName)
        .HasMaxLength(32)
        .IsUnicode(false);

    entity.HasMany(e => e.Widgets)
        .WithOne();
});
modelBuilder.Entity(Entity=>
{
entity.Property(e=>e.GroupName)
.HasMaxLength(32)
.IsUnicode(假);
entity.HasMany(e=>e.Widgets)
.WithOne();
});
以下是Henk评论中的DTO类:

public class DashboardConfigDTO
{
    public int DashboardType { get; set; }
    public int OrganisationId { get; set; }
    public IEnumerable<WidgetGroupDTO> WidgetGroups { get; set; }
}

public class WidgetGroupDTO
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int TabOrder { get; set; }
    public IEnumerable<WidgetConfigDTO> Widgets { get; set; }
}

public class WidgetConfigDTO
{
    public int IndicatorId { get; set; }
    public string ScopeId { get; set; }
    public string ParentScopeId { get; set; }
    public int WidgetType { get; set; }
    public int WidgetSize { get; set; }
    public int Order { get; set; }
}
公共类仪表板配置为
{
public int仪表板类型{get;set;}
公共int组织ID{get;set;}
公共IEnumerable WidgetGroups{get;set;}
}
公共类WidgetGroupDTO
{
公共int Id{get;set;}
公共字符串名称{get;set;}
public int TabOrder{get;set;}
公共IEnumerable小部件{get;set;}
}
公共类WidgetConfigDTO
{
公共int指示符{get;set;}
公共字符串ScopeId{get;set;}
公共字符串ParentScopeId{get;set;}
公共int WidgetType{get;set;}
public int WidgetSize{get;set;}
公共整数顺序{get;set;}
}

无法复制。您使用的是什么EF核心版本?实体框架核心版本
2.1.8
和ASP.NET核心版本
2.1.1
我已经编辑了实体,以更接近我在项目中拥有的实体(继承)。不幸的是,我没有这样的环境。您可以尝试看看它是否在最新的EF Core 2.2中复制,您可以将DashboardConfigDTO发布到类中吗?那里有很多可能出错的东西,无法复制。您使用的是什么EF核心版本?实体框架核心版本
2.1.8
和ASP.NET核心版本
2.1.1
我已经编辑了实体,以更接近我在项目中拥有的实体(继承)。不幸的是,我没有这样的环境。您可以尝试看看它是否在最新的EF Core 2.2中复制,您可以将DashboardConfigDTO发布到类中吗?那里有很多可能出错的地方。