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 :
context.Thetable
?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,
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;
}
}
User contributions licensed under CC BY-SA 3.0