Asked by:
Entity Framework take about 30 second to do login ?

Question
-
User-1846805900 posted
Hi
i have and MVC site with API, every thing works fine - i use Code First From Database
but when i try login it's take about 30 second to do the login and i don't know why ??!!
i use this code to login (user enter his RepID - then in code get user email by RepID then login)
// POST: /Account/Login [HttpPost] [AllowAnonymous] [ValidateAntiForgeryToken] public async Task<ActionResult> Login(LoginViewModel model, string returnUrl) { if (!ModelState.IsValid) { return View(model); } // This doesn't count login failures towards account lockout // To enable password failures to trigger account lockout, change to shouldLockout: true string email = db.AspNetUsers.Where(i => i.RepID == model.RepID).FirstOrDefault().Email; SignInStatus result = await SignInManager.PasswordSignInAsync(email, model.Password, model.RememberMe, shouldLockout: false); switch (result) { case SignInStatus.Success: return RedirectToLocal(returnUrl); case SignInStatus.LockedOut: return View(viewName: "Lockout"); case SignInStatus.RequiresVerification: return RedirectToAction(actionName: "SendCode", routeValues: new { ReturnUrl = returnUrl, RememberMe = model.RememberMe }); case SignInStatus.Failure: default: ModelState.AddModelError(key: "", errorMessage: "Invalid login attempt."); return View(model); } }
so please how can i make it done faster ?
Saturday, January 23, 2016 8:04 AM
All replies
-
User1633621018 posted
Hi Amin,
It seems your table is having bulky data. I would recommend you to: Create index on that column.
Try changing your code with below line and see if it makes any difference. I don't know how much it will be helpful w/o looking your table schema, but its worth to try at least.
string email = db.AspNetUsers.First(i => i.RepID == model.RepID).Email;
Saturday, January 23, 2016 1:02 PM -
User-1846805900 posted
i try it but it's still the same nothing happen, i use AspNetUsers table as it created i don't change any thing on it - did entity check if database is found or not each time first run ? may that make it slow - so what i can do for that ?
Sunday, January 24, 2016 7:56 PM -
User-1846805900 posted
any updates ...
Tuesday, January 26, 2016 9:03 AM -
User-986267747 posted
Hi a.amin,
string email = db.AspNetUsers.Where(i => i.RepID == model.RepID).FirstOrDefault().Email; SignInStatus result = await SignInManager.PasswordSignInAsync(email, model.Password, model.RememberMeI'm not sure if the execution time of these queries is too long and it caused this problem. You could refer to the following link to try to check the execution time of these queries. You need to makr sure where we spend lots of time.
http://stackoverflow.com/questions/15107992/performance-analyze-ado-net-and-entity-framework
I suspect that you use the await keyword and we need to spend more time, the method called PasswordSignInAsync does lots of steps.
http://tech.trailmax.info/2014/06/asp-net-identity-user-lockout/
Best Regards,
Klein zhang
Wednesday, January 27, 2016 7:15 AM -
User-821857111 posted
Does it always take 30 seconds for every user or just the first user to log in after the application first starts? If it is just the first user, it may be because of some kind of initialisation from Entity Framework. What does the code for your DbContext look like?
Wednesday, January 27, 2016 7:40 AM -
User753101303 posted
Hi,
Did you really found this is Entity Framework or is this an assumption? If not done yet what if you time your EF statements to see if it really happens here?
Wednesday, January 27, 2016 8:30 AM -
User-921158202 posted
Hi,
actually i have the same isssue , but like Mikesdotnetting has pointed it happen only with the first EF query . (about 10 sec)
after that , if i try a second time to run the same EF code , it's instantaneous .
i am using EF 6.1.3 in a DB First app, and my model contain less than 10 entities
any idea on how to improve that ?
Thursday, January 28, 2016 12:11 AM -
User-821857111 posted
any idea on how to improve that ?There will always be a certain amount of bootstrapping going on with the first request to a web app. The server needs to load it into an app domain. Views or Web Forms might need compiling depending on your publishing settings and you may have a DbContext initialiser in place which will validate the model against the current schema (which takes time). You can turn that off: http://www.entityframeworktutorial.net/code-first/turn-off-database-initialization-in-code-first.aspxThursday, January 28, 2016 8:45 AM -
User-1846805900 posted
Does it always take 30 seconds for every user or just the first user to log in after the application first starts? If it is just the first user, it may be because of some kind of initialisation from Entity Framework. What does the code for your DbContext look like?
Hi
if i try to login with user it takes about 30 Second to do login and redirect me to home page (its and empty view) - i try logout and then try login with same user its done fast
then i try to logout and try other user its take same time again 30 second ?
my DbContext code is:
public partial class SedicoDBContext : DbContext { public SedicoDBContext() : base("name=SedicoDBContext") { } public virtual DbSet<AspNetRole> AspNetRoles { get; set; } public virtual DbSet<AspNetUserRole> AspNetUserRoles { get; set; } public virtual DbSet<AspNetUser> AspNetUsers { get; set; } public virtual DbSet<Doctor> Doctors { get; set; } public virtual DbSet<DoctorsTerritory> DoctorsTerritories { get; set; } public virtual DbSet<DoctorsVisit> DoctorsVisits { get; set; } public virtual DbSet<LineDoctor> LineDoctors { get; set; } public virtual DbSet<Line> Lines { get; set; } public virtual DbSet<LineUser> LineUsers { get; set; } public virtual DbSet<Maps_Pos> Maps_Pos { get; set; } public virtual DbSet<MRepTerritory> MRepTerritories { get; set; } public virtual DbSet<Territory> Territorys { get; set; } public virtual DbSet<Title> Titles { get; set; } public virtual DbSet<UserManager> UserManagers { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<AspNetRole>() .HasMany(e => e.AspNetUserRoles) .WithRequired(e => e.AspNetRole) .HasForeignKey(e => e.RoleId); modelBuilder.Entity<AspNetUser>() .HasMany(e => e.LineUsers) .WithRequired(e => e.AspNetUser) .HasForeignKey(e => e.UserID) .WillCascadeOnDelete(false); modelBuilder.Entity<AspNetUser>() .HasMany(e => e.MRepTerritories) .WithRequired(e => e.AspNetUser) .HasForeignKey(e => e.UserID) .WillCascadeOnDelete(false); modelBuilder.Entity<AspNetUser>() .HasMany(e => e.UserManagers) .WithOptional(e => e.AspNetUser) .HasForeignKey(e => e.UserID); modelBuilder.Entity<Doctor>() .HasMany(e => e.Maps_Pos) .WithRequired(e => e.Doctor) .WillCascadeOnDelete(false); modelBuilder.Entity<DoctorsTerritory>() .HasMany(e => e.Maps_Pos) .WithOptional(e => e.DoctorsTerritory) .HasForeignKey(e => e.TerritoryMainID); modelBuilder.Entity<Territory>() .HasMany(e => e.DoctorsTerritories) .WithRequired(e => e.Territory) .WillCascadeOnDelete(false); modelBuilder.Entity<Territory>() .HasMany(e => e.MRepTerritories) .WithRequired(e => e.Territory) .WillCascadeOnDelete(false); modelBuilder.Entity<Title>() .HasMany(e => e.AspNetUsers) .WithRequired(e => e.Title) .WillCascadeOnDelete(false); } }
Friday, January 29, 2016 9:26 PM -
User-1846805900 posted
any updates ....
Wednesday, February 3, 2016 8:35 AM -
User-1376495155 posted
I have had this happen to me recently (for no apparent reason) and have a hunch as to why it happens. We have had it happen on some dev machines, and not others, we have rolled back to entity framework 6.1.2, which fixed it for a while, and then it randomly came back. Can you please answer the following?
Is this a .net or .net core project?
Did anyone in you (or your team) install visual studio 2017?
Does the problem persist in RELEASE mode?
Are you running SQL server 2014 or 2016?
Can you put a breakpoint in the creation of dbcontext ? (ie. in the below code) and see if it is the CONNECTION to the database which is taking a long time, rather than the login query?
public SedicoDBContext() : base("name=SedicoDBContext") { }
Tuesday, January 31, 2017 3:07 PM -
User684407203 posted
Hello, I'm also facing this delay checking using below method
var result = await SignInManager.PasswordSignInAsync(model.Username, model.Password, model.RememberMe, shouldLockout: false);
every time getting late to check 30 sec nearly when logging. Anyone PLease help me out this. I used MySQL DB.
Thursday, March 2, 2017 8:11 PM -
User-2097295820 posted
Hi Mike,
What if issam1975 precompiles is codes. Can this help. With this the framework would not have to keep loading DAL layer. cause every thing is just been set.
Monday, March 6, 2017 12:04 AM -
User511735230 posted
My experience is that the login is very fast, subsecond. However, when you debug the app on localhost, the compilation process of the next page can be very slow, as nothing is precompiled. I find there is huge difference once the site is published to IIS, even in debug mode. This can easily explain your 30 seconds, if the page where you redirect to is complex.
Sunday, June 4, 2017 11:29 AM -
User873319012 posted
so please how can i make it done faster ?I know this is an old thread but I've just finished speeding up one of my ASP.NET MVC projects that took 10 (TEN!) seconds to log in to.
Originally, the project was based on the Web Application project template of Visual Studio 2017 with user authentication checked. Which means that ASP.NET Identity and EF are used, by default.
After being fed up with how long it would take to not only log in but also navigate from one view to another (all views involve fetching data from the database) I decided to remove ASP.NET Identity and Entity Framework altogether and use Form Authentication and plain old asynchronous ADO.NET, instead.
To cut a long story short let me tell you right away that it now takes up to 1.5 (ONE AND A HALF!) seconds to log in and loading the views is almost instant (less than a sec).
I don't know about you but my customer is much much happier with such a blazing fast web application than I ever was using bloated technologies.
Thursday, August 15, 2019 6:37 PM