通过外键从相关表中检索数据

时间:2016-12-15 16:41:41

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

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

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

1 个答案:

答案 0 :(得分:0)

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

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

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