簡體   English   中英

初始化后,“模型”是否從表中讀取所有行? (性能)

[英]Does 'Model' reads all the rows from the table when initialized? (performance)

我有一個名為“ AccountModel”的模型,它具有public virtual DbSet<Account> Account { get; set; } public virtual DbSet<Account> Account { get; set; } public virtual DbSet<Account> Account { get; set; }屬性。 我在Linq使用它。 我只是想知道,這是否意味着模型會讀取表中的所有行?

例如,我只想執行(基本)登錄操作,首先我需要知道用戶名和密碼對的存在。 我可以使用簡單的sql查詢( SELECT Id, Password FROM Accounts WHERE Username = 'test' )進行操作,也可以使用Linqmodel.Account.First(x => x.Username == 'test')

有什么建議么?

我只是想知道,這是否意味着模型會讀取表中的所有行?

不,它只是創建一個從您的屬性“帳戶”到存儲“ Account對象的表的映射。 除非您執行消耗數據的操作,否則您實際上不會讀取數據。

using (MyContext ctx = new MyContext())
{
    var result = ctx.Account.Where(a => a.Category == "Prime");

甚至不會讀取任何數據。 直到您實際對result有所作為

    int primeAccounts = result.Count();

實際讀取任何數據。

例如,我只想執行(基本)登錄操作,首先我需要知道用戶名和密碼對的存在

你可以做類似的事情

bool valid = ctx.Account
                .Where(a => a.Username == 'test' && 
                       a.Password = 'HopefullyAHashAndNotAnActualPlaintextPassword')
                .Any();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM