Initial load and performance issue using entity framework 6

2

I used Entity Framework 6 in my application with database first approach. I added a logging method in the context's constructor :

Database.Log = sql=>Debug.WriteLine(sql);

It seems that in each instanciation of new context, the structures and datas of the tables are loaded. This caused performance troubles and the load of home interface takes a couple of secondes.

So I need to know :

  1. Is it the default behaviour of entity framework 6 : When I instanciate the context all tables and views in the model will be loaded in the memory ( structure and data) ?
  2. If it is true, How can I change this by loading only the structure of the table, the datas will be loaded when I make a call like this context.Thetable ?
  3. If it is not possible to change this default behaviour, the implementation of Singleton for the Dbcontext is it a good way to fix this problem ?

Edit

this is my code : I have a console application in which I added my EF model

 class Program
    {
        static void Main(string[] args)
        {
            Action empBasicAction ;
            Console.WriteLine("Début de programme");

            Console.WriteLine("Chargement de context1 ......");
            empBasicAction = () => { 
                Entities contexte = new Entities();
            };
            Console.WriteLine("fin de chargement1, temps = " + CalculateTime(empBasicAction));

            Console.WriteLine("Chargement de context2 ......");
            empBasicAction = () => { 
                Entities contexte = new Entities();
            };
            Console.WriteLine("fin de chargement2, temps = " + CalculateTime(empBasicAction));


            Console.WriteLine("Chargement de context3 ......");
             empBasicAction = () =>
            {
                Entities contexte = new Entities();
            };

            Console.WriteLine("fin de chargement3, temps = " + CalculateTime(empBasicAction));


            Console.WriteLine("Sélection sur une table ......");
            empBasicAction = () =>
            {
                Entities contexte1 = new Entities();
                contexte1.fsign_fiche_signaletique.Count();
            };

            Console.WriteLine("fin de sélection, temps = " + CalculateTime(empBasicAction));

            Console.WriteLine("Sélection2 sur une table ......");
            empBasicAction = () =>
            {
                Entities contexte1 = new Entities();
                contexte1.fsign_fiche_signaletique.Count();
            };

            Console.WriteLine("fin de sélection2, temps = " + CalculateTime(empBasicAction));

            Console.ReadLine();


        }

        public static long CalculateTime(Action  t)
        {
            Stopwatch alarme = new Stopwatch();
            alarme.Start();
            t();
            alarme.Stop();
            return alarme.ElapsedMilliseconds;

        }
    }

the Console output

 Début de programme
Chargement de context1 ......
fin de chargement1, temps = 434
Chargement de context2 ......
fin de chargement2, temps = 0
Chargement de context3 ......
fin de chargement3, temps = 0
Sélection sur une table ......
fin de sélection, temps = 4265
Sélection2 sur une table ......
fin de sélection2, temps = 19

The output

Opened connection at 12/02/2016 12:26:49 +01:00

SELECT 
    [GroupBy1].[A1] AS [C1]
    FROM ( SELECT 
        COUNT(1) AS [A1]
        FROM [dbo].[fsign_fiche_signaletique] AS [Extent1]
    )  AS [GroupBy1]


-- Executing at 12/02/2016 12:26:52 +01:00

-- Completed in 10 ms with result: SqlDataReader



Closed connection at 12/02/2016 12:26:52 +01:00

Opened connection at 12/02/2016 12:26:52 +01:00

SELECT 
    [GroupBy1].[A1] AS [C1]
    FROM ( SELECT 
        COUNT(1) AS [A1]
        FROM [dbo].[fsign_fiche_signaletique] AS [Extent1]
    )  AS [GroupBy1]


-- Executing at 12/02/2016 12:26:52 +01:00

-- Completed in 5 ms with result: SqlDataReader



Closed connection at 12/02/2016 12:26:52 +01:00

The program '[4316] ConsoleApplication1.vshost.exe' has exited with code -1073741510 (0xc000013a).

Thanks,

c#
.net
performance
entity-framework
memory-management
asked on Stack Overflow Feb 12, 2016 by Lamloumi Afif • edited Feb 12, 2016 by Lamloumi Afif

1 Answer

1

It seems that in each instanciation of new context, the structures and datas of the tables are loaded.

FALSE

1: FALSE, this is you, instructing it to pull data.

2: 1 was false so, might sound not helpful, but code it that way.

3: Your assumptions were wrong so this doesn't need an answer.

For SO(stack overflow) to be helpful and you to perceive it as being so... I suggest you post code... so we can help point out WHY its slow.

But i have answered your questions ;-)

Update

why not just

class Program
{
    static void Main(string[] args)
    {
        Entities context = new Entities();

        Stopwatch alarme = new Stopwatch();
        alarme.Start();

        var fSignFicheSignaletiqueCount = context.fsign_fiche_signaletique.Count();

        alarme.Stop();
        return alarme.ElapsedMilliseconds;
    }
}
answered on Stack Overflow Feb 12, 2016 by Seabizkit • edited Feb 12, 2016 by Lamloumi Afif

User contributions licensed under CC BY-SA 3.0