简体   繁体   English

实体框架Linq查询何时命中Sql Databse?

[英]When does Entity framework Linq query Hits Sql Databse?

i'm new in entity framework.Below is my code, 我是实体框架的新手。下面是我的代码,

So in my code i have created object of my db context and then i have a query 'queryForAuthentication' and in that i have used two tables 'conDb.SystemMasters' and joined with conDb.SystemAdminMasters , so will hit twice or how does it manage . 因此,在我的代码中,我创建了我的数据库上下文的对象,然后有一个查询“ queryForAuthentication”,因为我使用了两个表“ conDb.SystemMasters”并与conDb.SystemAdminMasters联接,因此将命中两次或如何管理。 i want to know when does entity framework will hit in to database ? 我想知道实体框架何时会进入数据库?

 QuizzrEntities conDb = new QuizzrEntities();
 List<OnLoginData> lstOnLogoonData = new List<OnLoginData>();
 string userpassWordHash = string.Empty;
 var queryForAuthentication =from systemObj in conDb.SystemMasters
                             where systemObj.StaffPin == dminLoginInput.StaffPin
                             join admin in conDb.SystemAdminMasters on systemObj.SystemId equals admin.SystemID
                             select new 
                            {
                             admin.PasswordSalt,
                             admin.PasswordHash, 
                             systemObj.StaffPin,
                             admin.UserName, 
                             admin.SystemID 
                            };
 if (queryForAuthentication.Count() > 0)
                    {
                        CheckStaffPin = true;
                        var GetUserUsingUsernamePasword = queryForAuthentication.Where(u => u.UserName.ToLower() == AdminLoginInput.UserName.ToLower());
                        if (GetUserUsingUsernamePasword.ToList().Count == 1)
                        {
                            checkuserName = true;
                            string DBPasswordSalt = queryForAuthentication.ToList()[0].PasswordSalt,
                                   DBPasswordHash = queryForAuthentication.ToList()[0].PasswordHash,
                                   StaffPin = queryForAuthentication.ToList()[0].StaffPin;
                            userpassWordHash = Common.GetPasswordHash(AdminLoginInput.Password, DBPasswordSalt);
                            if ((DBPasswordHash == userpassWordHash) && (AdminLoginInput.StaffPin.ToLower() == StaffPin.ToLower()))
                            {
                                checkPassword = true;
                                CheckStaffPin = true;
                            }
                            else if (DBPasswordHash == userpassWordHash)
                            {
                                checkPassword = true;
                            }
                            else if (AdminLoginInput.StaffPin.ToLower() == StaffPin.ToLower())
                            {
                                CheckStaffPin = true;
                            }


                        }

                    }

So in my code i have created object of my db context and then i have a query 'queryForAuthentication' and in that i have used two tables 'conDb.SystemMasters' and joined with conDb.SystemAdminMasters , so will hit twice or how does it manage . 因此,在我的代码中,我创建了我的数据库上下文的对象,然后有一个查询“ queryForAuthentication”,因为我使用了两个表“ conDb.SystemMasters”并与conDb.SystemAdminMasters联接,因此将命中两次或如何管理。 i want to know when does entity framework will hit in to database ? 我想知道实体框架何时会进入数据库?

It's hits the database whenever you fire a query. 每当您触发查询时,它就会命中数据库。 And query will be fired whenever you perform ToList, First, FirstOrDefault etc. operation. 只要执行ToList,First,FirstOrDefault等操作,查询就会被触发。 Till then it only builds the query. 直到那时,它只会构建查询。

try Code 尝试代码

 QuizzrEntities conDb = new QuizzrEntities();
 List<OnLoginData> lstOnLogoonData = new List<OnLoginData>();
 string userpassWordHash = string.Empty;
 var queryForAuthentication =(from systemObj in conDb.SystemMasters
                         where systemObj.StaffPin == dminLoginInput.StaffPin
                         join admin in conDb.SystemAdminMasters on systemObj.SystemId equals admin.SystemID
                         select new 
                        {
                        PasswordSalt= admin.PasswordSalt,
                        PasswordHash= admin.PasswordHash, 
                        StaffPin= systemObj.StaffPin,
                        UserName= admin.UserName, 
                       SystemID =  admin.SystemID 
                        }).FirstOrDefault();
If(queryForAuthentication !=null)
{
-----------------
-----------------
*****Your Code*******
}

In entity framework also work with sql query based. 在实体框架中还可以与基于sql的查询一起使用。 If you are disconnected using .ToList() then only the record taken from local otherwise it's works as DBQuery. 如果使用.ToList()断开连接,则只有从本地获取的记录才可以用作DBQuery。 if you check the result view in debug view it's Execute the Query and Return the data. 如果在调试视图中检查结果视图,则执行查询并返回数据。

If you are processing the data is discontinued from the base it's executed finally where you want the result. 如果您正在处理数据,那么将从基中中断数据,然后在希望得到结果的位置最终执行该数据。

You processing data locally then you can disconnect the connection between linq and sql using call .ToList(). 您在本地处理数据,然后可以使用.ToList().断开linq和sql之间的连接.ToList(). it's Processing only one time the Object weight is high more than query. 它只处理对象权重比查询高一倍的时间。

var queryForAuthentication =from systemObj in conDb.SystemMasters
                         where systemObj.StaffPin == dminLoginInput.StaffPin
                         join admin in conDb.SystemAdminMasters on systemObj.SystemId equals admin.SystemID
                         select new 
                        {
                         admin.PasswordSalt,
                         admin.PasswordHash, 
                         systemObj.StaffPin,
                         admin.UserName, 
                         admin.SystemID 
                        }.ToList() ; // It will fetch the data
//Check from inmemory collection 
if (queryForAuthentication.Count > 0)

//As you already have the data in memory this filter applied against inmemory collection not against database.
var GetUserUsingUsernamePasword = queryForAuthentication
             .Where(u =>u.UserName.ToLower() == AdminLoginInput.UserName.ToLower());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM