EF Core ThenInclude 外键值与 1:n relationshop
EF Core ThenInclude foreign key values with 1:n relationshop
我有一个 Transaction
有多个 TransactionLines
。 transactionLine 有 SubCategory
,SubCategory 有 Category
.
我正在尝试获取交易列表。我希望能够列出交易及其分配的类别。通常一个 Transaction 有一个 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
。只有 Sum
、First
等
我正在努力实现:
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 class 有一个 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
}
交易行
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; }
}
子类别
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; }
}
尝试以下操作:
.ThenInclude(x=>x.Subcategory)
.ThenInclude(x=>x.Category)
ThenInclude() "works" 与先前选择的 属性,在您的情况下,第一种情况是 TransactionLine
,第二种情况是 Subcategory
.
不要介意自动完成没有为您提供 TransactionLine/Subcategory 的正确属性。在这种情况下,我认为它会被 2 个 ThenInclude() 重载混淆。
我有一个 Transaction
有多个 TransactionLines
。 transactionLine 有 SubCategory
,SubCategory 有 Category
.
我正在尝试获取交易列表。我希望能够列出交易及其分配的类别。通常一个 Transaction 有一个 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
。只有 Sum
、First
等
我正在努力实现:
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 class 有一个 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
}
交易行
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; }
}
子类别
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; }
}
尝试以下操作:
.ThenInclude(x=>x.Subcategory)
.ThenInclude(x=>x.Category)
ThenInclude() "works" 与先前选择的 属性,在您的情况下,第一种情况是 TransactionLine
,第二种情况是 Subcategory
.
不要介意自动完成没有为您提供 TransactionLine/Subcategory 的正确属性。在这种情况下,我认为它会被 2 个 ThenInclude() 重载混淆。