【问题标题】:Entity Framework Core Select Outer Join实体框架核心选择外连接
【发布时间】:2018-12-19 04:37:39
【问题描述】:

是否有一种简单的方法可以在 EF Core 的选择表达式中包含可为空的导航?

我的模型是这样的

public class RootVO
{
    [Key]
    public int Id { get; set; }

    [Required]
    [StringLength(200)]
    public string Description { get; set; }

    public int? RelationId { get; set; }
    [ForeignKey(nameof(RelationId))]
    public RelationVO Relation { get; set; }
}

public class RelationVO
{
    [Key]
    public int Id { get; set; }

    [Required]
    [StringLength(200)]
    public string Property1 { get; set; }

    [Required]
    [StringLength(200)]
    public string Property2 { get; set; }

    public ICollection<RootVO> RootRelations { get; set; }
}

当我加载数据时,我只想选择某种属性。目前我的表情是这样的:

Expression<Func<RootVO, RootVO>> selectExpr = m => new RootVO
{
    Id = m.Id,
    Description = m.Description,
    Relation = m.Relation != null ? new RelationVO
    {
        Id = m.Relation.Id,
        Property1 = m.Relation.Property1                        
    } : null
};

var result = context.Roots.Select(selectExpr).ToList();

有没有更简单的方法来处理关系选择?

编辑

也许这里的一些背景会有所帮助:

我有一个巨大的对象,它有很多列和关系,一些有内部连接,一些有外部连接。此查询由 UI 上的数据网格访问,该数据网格可以根据用户选择具有动态列。为了提高性能,我编写了一个类,它将根据所选列动态构建选择表达式。现在它正在工作,但是当外部连接由于空引用异常而为空时,我遇到了麻烦。

表达式的调试视图可能如下所示:

.New IVA.Core.Data.Models.StockMovementLogVO(){
    SequenceNo = $m.SequenceNo,
    PostingPeriodId = $m.PostingPeriodId,
    TransactionDate = $m.TransactionDate,
    FinancialYear = $m.FinancialYear,
    FinancialYearPeriod = $m.FinancialYearPeriod,
    VoucherDate = $m.VoucherDate,
    ItemQuantity = $m.ItemQuantity,
    BuCode = $m.BuCode,
    LocationStructure = .New IVA.Core.Data.Models.LocationStructureVO(){
        Id = ($m.LocationStructure).Id,
        Description = ($m.LocationStructure).Description
    },
    BookingType = .New IVA.Core.Data.Models.BookingTypeVO(){
        Id = ($m.BookingType).Id,
        Description = ($m.BookingType).Description
    },
    PartnerStockLocationType = .New IVA.Core.Data.Models.StockLocationTypeVO(){
        Id = ($m.PartnerStockLocationType).Id,
        Description = ($m.PartnerStockLocationType).Description
    },
    StockLocationType = .New IVA.Core.Data.Models.StockLocationTypeVO(){
        Id = ($m.StockLocationType).Id,
        Description = ($m.StockLocationType).Description
    }
}

StockLocationType 和 PartnerStockLocationType 是外部联接,如果它们为 null,则查询无法执行。

【问题讨论】:

  • 最简单的方法是什么意思?您是否尝试过Select 而没有通过Expressions
  • 为什么不直接查询context.RootsInclude(r =&gt; r.Relation)
  • 我只想加载关系对象的某些属性而不是全部,所以我必须创建自己的选择语句。
  • @NiQu 无论如何,EF 将使用 JOIN 与第二个表。我们可以使用Select(x =&gt; new RootVo {}) 不使用表达式来做到这一点。您是否期待任何其他类型的解决方案?
  • 我已经扩展了描述。也许这有助于理解我的问题。

标签: entity-framework asp.net-core entity-framework-core


【解决方案1】:

我现在更改了我的表达式构建器,它将通过包含一个空引用检查来处理外部连接。表达式现在看起来像这样:

.New IVA.Core.Data.Models.StockMovementLogVO(){
    SequenceNo = $m.SequenceNo,
    PostingPeriodId = $m.PostingPeriodId,
    TransactionDate = $m.TransactionDate,
    FinancialYear = $m.FinancialYear,
    FinancialYearPeriod = $m.FinancialYearPeriod,
    VoucherDate = $m.VoucherDate,
    ItemQuantity = $m.ItemQuantity,
    BuCode = $m.BuCode,
    LocationStructure = .New IVA.Core.Data.Models.LocationStructureVO(){
        Id = ($m.LocationStructure).Id,
        Description = ($m.LocationStructure).Description
    },
    BookingType = .New IVA.Core.Data.Models.BookingTypeVO(){
        Id = ($m.BookingType).Id,
        Description = ($m.BookingType).Description
    },
    PartnerStockLocationType = .If ($m.PartnerStockLocationType != null) {
        .New IVA.Core.Data.Models.StockLocationTypeVO(){
            Id = ($m.PartnerStockLocationType).Id,
            Description = ($m.PartnerStockLocationType).Description
        }
    } .Else {
        null
    },
    StockLocationType = .If ($m.StockLocationType != null) {
        .New IVA.Core.Data.Models.StockLocationTypeVO(){
            Id = ($m.StockLocationType).Id,
            Description = ($m.StockLocationType).Description
        }
    } .Else {
        null
    }
}

编辑

如果有人对它的外观感兴趣,我已经创建了一个使用该类的存储库。

https://github.com/NQuirmbach/DynamicQueryBuilder

【讨论】:

    猜你喜欢
    • 2021-11-18
    • 1970-01-01
    • 2021-11-15
    • 2021-09-03
    • 2020-07-08
    • 1970-01-01
    • 1970-01-01
    • 2017-06-15
    • 2021-07-14
    相关资源
    最近更新 更多