ASP.net MVC 4 site gets slow on first request to database

485 views Asked by At

I have an ASP.net MVC 4 site and it gets slow on the first request. I tried breakpoint while running the application. During the login process it almost stays for a minute at my first database query:

  var InstnCode = form["code"].ToString();
  var ComAccount = Context.Companies.Where(x => x.CompanyCode == InstnCode); 

After that everything runs smoothly.

Why is it so how can I rectify this process. Due to this problem sometimes I get a Server Timeout error.

  [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
       public ActionResult Login(UserProfile model, string returnUrl, FormCollection form)
       {

        var InstnCode = form["code"].ToString();
        var ComAccount = Context.Companies.Where(x => x.CompanyCode == InstnCode);

        if (ComAccount.Any())
        {
            var modelvalue =
                (from d in Context.UserProfiles
                    where d.UserName == model.UserName && d.Password == model.Password && d.Company.CompanyCode == InstnCode
                    select d).FirstOrDefault();


            if (modelvalue != null)
            {
                string code = null;

                Session["UName"] = modelvalue.UserName;
                Session["Theme"] = modelvalue.Theme;
                Session["InstnName"] = modelvalue.Company.CompanyName;
                Session["Role"] = modelvalue.Role.RoleName;
                Session["StartUp"] = modelvalue.StartUp;

                var permission =
                    Context.AccountPermissions.Where(x => x.RoleId == modelvalue.RoleId)
                        .AsQueryable()
                        .FirstOrDefault();

                if (permission != null)
                {
                    SetSessions(permission, "yes");
                }
                else
                {
                    SetSessions(permission, "no");
                }

                if (modelvalue.CompanyId != 0 && modelvalue.StaffId == null && modelvalue.StudentProfileId == null)
                {
                    Session["ComID"] = modelvalue.CompanyId;
                    code = modelvalue.Company.CompanyCode;

                }
                else if (modelvalue.CompanyId != 0 && modelvalue.StudentProfileId != null)
                {
                    var student =
                        (from d in Context.StudentProfiles
                            where d.StudentProfileId == modelvalue.StudentProfileId
                            select d).FirstOrDefault();
                    code = student.Company.CompanyCode;
                    Session["ComID"] = student.CompanyId;
                }
                else if (modelvalue.CompanyId != 0 && modelvalue.StaffId != null)
                {
                    var staff =
                        (from d in Context.Staff where d.StaffId == modelvalue.StaffId select d).FirstOrDefault();
                    code = staff.Company.CompanyCode;
                    Session["ComID"] = staff.CompanyId;
                    Session["StaffID"] = staff.StaffId;
                }


                    return RedirectToLocal(returnUrl);



            }
            else
            {
                ModelState.AddModelError("", "The user name or password provided is incorrect");
                return View(model);
            }

        }
        ModelState.AddModelError("", "The institution code provided is incorrect");
        return View(model);
    }

This is my login function. I'm using @using (Html.BeginForm("Login", "Account", FormMethod.Post, new { enctype = "multipart/form-data" })) for my login form.

Any help will be greatly appreciated. Thanks in advance

0

There are 0 answers