我在显式加载特定关系的相关数据时遇到了一些问题。
有问题的课程定义如下:
public abstract class Wallet
{
#region Database Properties
[Key]
public string Address { get; set; }
[Required]
public DateTime CreatedDate { get; set; } = DateTime.UtcNow;
#endregion
#region Navigation Properties
public List<WalletTransaction> WalletTransactions { get; set; }
#endregion
}
public class UserWallet : Wallet
{
[Required]
public int UserId { get; set; }
}
public class WalletTransaction
{
#region Database Properties
[Key]
[Required]
public int Id { get; set; }
[ForeignKey("Wallet")]
[Required]
public string WalletId { get; set; }
[Required]
public string Currency { get; set; }
[Required]
[Column(TypeName = "decimal(28,18)")]
public decimal Amount { get; set; }
[Required]
public string Description { get; set; }
#endregion
#region Navigation Properties
public Wallet Wallet { get; set; }
#endregion
}
然后我尝试获取电子钱包的特定实例,包括此处的WalletTransactions
:
public static UserWallet GetById(string id)
{
using (var db = EthereumWalletDbContextFactory.Create())
{
var wallet = db.UserWallets
.Single(f => f.Address == id);
db.Entry(wallet)
.Collection(c => c.WalletTransactions)
.Load();
return wallet;
}
}
我得到的是一个包含0个交易的Wallet实例。我在SQL Server上运行了一个分析器,并确认没有尝试查找WalletTransactions
。
有什么想法吗?