0

After starting my application for the first time, my application is taking 7 seconds to insert just a single entity with 2 attributes.

I am using .NET Framework in a desktop application, Visual Studio 2019 Community edition, Entity Framework v6.0.0.0.

Am I making some mistake? Does any one have any idea?

This is my code:

UserService UserService = new UserService();

User userObj = new User();
userObj.Name = "Arsi";
userObj.Age = 25;

// db.Users.Add(userObj);
// db.SaveChanges();
UserService.InsertUser(userObj);

CRUD_TestingEntities db = new CRUD_TestingEntities();

public void InsertUser(User user)
{
    if (user != null)
    {
        db.Users.Add(user);
        db.SaveChanges();
    }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • EF needs time to start up. If it's slow a second time in the same running application _then_ you should investigate the insert performance itself. – Gert Arnold Mar 21 '20 at 16:19
  • i think it is starting fine but when the following line are executing they are taking all the time: db.Users.Add(user); db.SaveChanges(); – Muhammad Arsalan Mar 21 '20 at 16:21
  • can you suggest me some trick that it is started in the background for the first time and when insert first time it does not take such long time. – Muhammad Arsalan Mar 21 '20 at 16:22
  • @fwerther i am using sql server 2012 and it a local server with desktop application, and seriously telling i am just testing this with only one table in the database, which clears that the application is not a giant structural application. – Muhammad Arsalan Mar 21 '20 at 16:25
  • 1
    No, impossible with this amount of info. As usual with performance issues. Too many moving parts. We know nothing of your application, architecture, db engine, db design, hardware, and so on and so forth. – Gert Arnold Mar 21 '20 at 16:25
  • @GertArnold it is database first application, – Muhammad Arsalan Mar 21 '20 at 16:26
  • Maybe this can help you out: https://stackoverflow.com/questions/30423838/entity-framework-very-slow-to-load-for-first-time-after-every-compilation – fwerther Mar 21 '20 at 16:26
  • Our comments crossed, but still, I wouldn't know where to begin. – Gert Arnold Mar 21 '20 at 16:27
  • If repeated inserts that that time, DO NOT BLAME EF. Intercept the SQL, look at the query plans. EF inserts a simple insert. If that takes long, start analyzing the database and fix the issues you find there. – TomTom Mar 21 '20 at 17:00

1 Answers1

0

You make one mistake - you blame EF for something it is not responsble. Like blaming a car for not being able to go fast - in a traffic jam.

If EF is slow at start, that is startup.

If that is slow repeatedly, then you need to intercept the SQL and fix the issue on the database. EF is just issuing a simple insert statement - if the DB takes long, something is wrong on the db. Not EF's fault.

TomTom
  • 61,059
  • 10
  • 88
  • 148