1

Everytime when I open the database connection, the very first interaction with the database takes a lot of time. From the second interaction on the "speed" is much higher. I think it's an optimization of the Entity Framework and it looks like something good. But it's still the same even if I'm working with more than one database. The very first interaction with the first database is slow. But the first interaction with the second database is fast. Why isn't the first interaction with another database slow too? Another problem is that the Initializer only works with the first database. (I think this problem is caused by this optimization)

Related to this question (asked by myself): Entity Framework 6 SetInitializer DbContext does not work for SQL Server Express and SQL Server Compact

Does anyone know how this optimization works and how to disable it? I don't actually work with the databases I compare them, for this case it would be nice to disable any optimization.

Community
  • 1
  • 1
0lli.rocks
  • 1,027
  • 1
  • 18
  • 31
  • 1
    Have you checked [this article](http://msdn.microsoft.com/en-us/library/cc853327.aspx) and [this post](http://stackoverflow.com/questions/13250679/how-to-warm-up-entity-framework-when-does-it-get-cold) ? – Yuliam Chandra Aug 18 '14 at 16:12
  • @YuliamChandra Thanks a lot for your helpful links! It's not the answer for my problem, because they want to improve the performance, but now I know what I am looking for. "How to force the database to get cold?" Thx! – 0lli.rocks Aug 19 '14 at 07:28

1 Answers1

1

I'm wrote an answer as comment is not appropriate.

For the speed Yuliam pointed you to the good link.

For the rest, I'm not sure to understand your problem.

For me the following code (pertinent extract)

class Program {
    static void Main(string[] args) {
        Database.SetInitializer<TestEFContext>(new DropCreateDatabaseAlways<TestEFContext>());

        String cs = @"Data Source=ALIASTVALK;Initial Catalog=TestEF;Integrated Security=True; MultipleActiveResultSets=True";
        using (TestEFContext ctx = new TestEFContext(cs)) {
            Console.WriteLine(ctx.Orders.Count());
        }

        cs = @"Data Source=ALIASTVALK;Initial Catalog=TestEF2;Integrated Security=True; MultipleActiveResultSets=True";
        using (TestEFContext ctx = new TestEFContext(cs)) {
            Console.WriteLine(ctx.Orders.Count());
        }            
    }
}

well create and recreate databases TestEF and TestEF2.

Did I misunderstand your question ?

===== Response to comment:

In this case both databases are always (re)created:

  • first run: creation
  • use SSMS to populate some data in some tables
  • second run
  • check tables content with SSMS : tables are empty <=> databases were recreated at second run.

Only trivial difference with your case: I use EF 6;

tschmit007
  • 7,559
  • 2
  • 35
  • 43
  • You understood my problem correctly. (Perfect summary! Thx) Your code looks quiet similar to mine (take a look at the linked post). The problem is that the second called databases is only (re)created if it not exists, otherwise it's not recreated. Doesn't matter which database is called first. – 0lli.rocks Aug 19 '14 at 07:21
  • Thanks for your help, my code looks like yours. I get the connections string out of the app.config but that should not be a problem. If I have the time I will rewrite my code and try various solution. – 0lli.rocks Aug 20 '14 at 07:26