实体框架核心 - 获取相关数据

时间:2018-04-16 12:57:54

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

我在显式加载特定关系的相关数据时遇到了一些问题。

有问题的课程定义如下:

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

有什么想法吗?

0 个答案:

没有答案