我正在努力为自己制作一个ebank,所以它并不那么严重。这是我的结构:
到目前为止我取得的成就:如果用户登录,并点击Ebank动作链接,为他/她显示所有数据(我的意思是如果登录 userId == 1 我用 userId == 2 显示所有数据,这是我想要改变的。)
public ActionResult Index()
{
var bankaccounts = db.BankAccounts.Include(b => b.Type).Include(b => b.Balance).Include(b => b.UserProfile);
return View(bankaccounts.ToList());
}
如果我是对的,这个“var bankaccounts”行是我必须改变的。如果我将断点放到“返回视图(bankaccounts.ToList())”行,我会从“bankaccounts”中获取以下数据:
{
SELECT
[Extent1].[BankAccountId] AS [BankAccountId],
[Extent1].[UserId] AS [UserId],
[Extent1].[AccountName] AS [AccountName],
[Extent1].[AccountType] AS [AccountType],
[Extent1].[AccountNumber] AS [AccountNumber],
[Extent1].[AccountStatus] AS [AccountStatus],
[Extent1].[AccountAvaiability] AS [AccountAvaiability],
[Extent2].[AccountType] AS [AccountType1],
[Extent2].[TypeName] AS [TypeName],
[Extent3].[BalanceId] AS [BalanceId],
[Extent3].[BalanceAmount] AS [BalanceAmount],
[Extent3].[TrustAmount] AS [TrustAmount],
[Extent4].[UserId] AS [UserId1],
[Extent4].[UserName] AS [UserName]
FROM [dbo].[BankAccount] AS [Extent1]
INNER JOIN [dbo].[Type] AS [Extent2] ON [Extent1].[AccountType] = [Extent2].[AccountType]
LEFT OUTER JOIN [dbo].[Balance] AS [Extent3] ON [Extent1].[BankAccountId] = [Extent3].[BalanceId]
INNER JOIN [dbo].[UserProfile] AS [Extent4] ON [Extent1].[UserId] = [Extent4].[UserId]}
问题:
我在代码中找不到任何真正的sql命令(由mvc自动生成),所以如果我是对的
答案 0 :(得分:1)
您正在使用像Entity Framework这样的提供程序,它提供从代码到数据库的映射。具体做法是:
ToList()
实现查询时,将执行Sql。在实现步骤之前,var bankaccounts
是IQueryable
。BankAccount
之类的实体。Where
表达式谓词:的
var userBankaccounts = db.BankAccounts
.Where(ba => ba.UserId == websecurity.CurrentUserId);
你看到的Sql是'真实的'(没有卷曲的{}括号) - 一旦你看到相当讨厌的表和派生表的别名为[Extentxxx]
,它将允许你快速检查你的Linq表达式正在执行所需的查询。