【问题标题】:Retrieving Data from a Related Table Via Foreign Keys通过外键从相关表中检索数据
【发布时间】:2016-12-15 16:41:40
【问题描述】:

我有一个包含四个表的实体框架模型,我们这里唯一需要关注的两个是 [AtmAccounts] 和 [Transactions]。 [AtmAccounts] 具有以下属性:[Id -Primary Key]、[AccountNumber]、[AccountBalance]、[UserId] 和 [AccTypeId]。 Transactions 具有以下内容:[Id- Primary Key]、[TransAmount]、[TransDate]、[AtmAccountId - Navigation Property] 和 [TransTypeId]。

我想获取特定帐户的交易列表并将其显示在屏幕上。为此,我需要从事务表中获取与存储在 AtmAccounts 中的 AtmAccountId 相对应的所有记录。我该怎么做?

【问题讨论】:

  • db.Transactions.Where(s=>s.AtmAccountId==someAccountId") ?
  • @JayRoberts 对你有用吗?

标签: sql asp.net-mvc entity-framework


【解决方案1】:

除非您删除了关系,否则您的外键关系应该已经在您的实体模型中。在您的代码中,Intellisense 应该显示这一点,例如:

var myList = (from x in db.AtmAccounts
             where x.Id==myspecifiedid
             select x.Transactions).ToList();

应该为您提供指定 AtmAccount 的关联交易。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-29
    • 2019-10-14
    • 2017-04-18
    • 2019-09-04
    • 1970-01-01
    • 1970-01-01
    • 2014-04-13
    • 1970-01-01
    相关资源
    最近更新 更多