EF Core然后通过1:n Relationshop包含外键值

时间:2019-09-10 06:00:51

标签: c# entity-framework linq ef-core-2.2

我有一个Transaction,其中有多个TransactionLines。 transactionLine具有SubCategory,SubCategory具有Category

我正在尝试获取交易清单。我希望能够列出交易及其分配给的类别。通常,一个事务有一个TransactionLine,但可以有多个,每个Line具有不同的类别。

我正在尝试在客户端Linq语句中执行此操作,但是我的“包含”让我失望了。

        var data = context.Transaction
            .Include(x => x.CreditAccount)
            .Include(x => x.DebitAccount)
            .Include(x => x.TransactionLines)
            .ThenInclude(x=>x.TransactionLines.SubCategory)
            .ThenInclude(x=>x.TransctionLines.SubCategory.Category)
            .Include(x => x.Budget)

ThenInclude行不正确。它不提供SubCategory。仅SumFirst等。

我正在努力实现:

SELECT 
FROM Transaction t
INNER JOIN TransactionLine tl ON tl.TransactionId = t.Id
INNER JOIN SubCategory sc ON sc.Id = tl.SubCategoryId
INNER JOIN Category c ON c.Id = sc.CategoryId

但是我似乎无法正确显示SubCategory部分。

我的TransactionLine类具有SubCategory对象。

 public virtual Subcategory Subcategory { get; set; }

但是似乎我无法访问它。任何帮助都会很棒。

实体:

交易:

internal class Transaction 
    {
        [Key, Required, Column(Order = 1)]
        public int Id { get; set; }

        [Required, Column(Order = 2)]
        public Guid ExternalId { get; set; }

        [Required, Column(TypeName = "date", Order = 3)]
        public DateTime Date { get; set; }

        [Column(Order = 4)]
        public string Description { get; set; }

        [Required, Column(Order = 3), InverseProperty("TransactionDebitAccount")]
        public virtual Account DebitAccount { get; set; } // Nav

        [Required, Column(Order = 4), InverseProperty("TransactionCreditAccount")]
        public virtual Account CreditAccount { get; set; } // Nav

        [Required]
        public virtual List<TransactionLine> TransactionLines { get; set; } //Nav
    }

TransactionLine

internal class TransactionLine
    {
        [Key, Required, Column(Order = 0)]
        public int Id { get; set; }

        [Required, Column(Order = 1)]
        public Guid ExternalId { get; set; }

        [Required, Column(Order = 2)]
        public virtual Transaction Transaction { get; set; } // Nav 

        [Column(Order = 5), Range(0.01, 999999.99, ErrorMessage = "Amount must be between 0.01 and 999,999.99")]
        public decimal Amount { get; set; }

        [Column(Order = 6)]
        public virtual Budget Budget { get; set; }

        [Column(Order = 7)]
        public virtual Subcategory Subcategory { get; set; }

    }

SubCategory

internal class Subcategory : EntityAudit
{
    [Required, Key]
    public int Id { get; set; }

    [Required]
    public Guid ExternalId { get; set; }

    [Required, StringLength(30, MinimumLength = 2, ErrorMessage = "Subcategory Name must be between 2 and 30 charactors")]
    public string Name { get; set; }

    [Required]
    public Category Category { get; set; }
}

1 个答案:

答案 0 :(得分:1)

尝试以下操作:

.ThenInclude(x=>x.Subcategory)
.ThenInclude(x=>x.Category)

ThenInclude()与先前选择的属性一起使用,在您的情况下,第一种情况为TransactionLine,在第二种情况下为Subcategory

不要介意“自动完成”没有为您提供TransactionLine /子类别的正确属性。在这种情况下,我认为它会因2个ThenInclude()重载而感到困惑。