7

I'm trying to reduce the startup time for tests against an EF 6x datastore. The tests are within a transaction and the db gets rolled back once done. I would appreciate any suggestions on how to retain an instance of the DbContext between test sessions so that EF doesn't have to go through the whole view generation process again?

I don't want to use mocks/fakes, non-Microsoft branch of EF and interactive views are already in place. Thank you.

4
  • 5
    You will have to provide some code and more detail on what it is you are doing, by that I mean how is this test project setup. Do you have multiple DbContext instances? One per test? One per class? One global one? You have to illustrate the initialization is slow so that it gives us insight as to what can be done to fix it. Commented Aug 27, 2016 at 10:38
  • A DbContext should be extremely lightweight in practice. E.g. in web applications, you have one such instance per user request. They really are (or at least should be) lightweight objects. Thus, the performance issues you experience may be from somewhere else. Perhaps a DbInitializer for your tests instead? Did you profile your code? Perhaps I am mistaken though... Commented Sep 2, 2016 at 14:47
  • You should not retain a DbContext instance and reuse it among different test sessions. That's not really different from reusing a DbContext, say, among different threads. Which is bad practice (not thread safe). Commented Sep 2, 2016 at 14:51
  • 1
    Cache the database in a Docker image, and start a new container every time? Commented Sep 3, 2016 at 0:39

2 Answers 2

1
+50

Different options. As you did not mentioned your aim of testing and there is not any code, the options are:

  1. If you are inserting many records into your tables, you can do a bulk insert. The best library for doing this is:EntityFramework.BulkInsert-ef6. You can install it through Nuget console.

  2. If you see slowness while working with data and you have many load/manipulation/save operations, you have to do in-memory operation as Sampath recommends.

  3. If you are loading data, just load the columns that you need. You also should use lazy loading option(which from your post, I think you know it well).

4.Some portion of the slowness could be because of the architecture of your database. The key column types have a considerable effect on Where operations!

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your response. The question is about retaining the same running instance of a DbContext between test sessions. Thank you.
0

I would like to recommend you to use in-memory data for that. I am also used this pattern and it is really well and very fast. This is the pattern where the industry recommended and trouble free in long run. Always try to use best practices when you develop a software app.

When writing tests for your application it is often desirable to avoid hitting the database. Entity Framework allows you to achieve this by creating a context – with behavior defined by your tests – that makes use of in-memory data.

Here is the article about how to do that :Testing with a mocking framework

Another article for you : Unit testing in C# using xUnit, Entity Framework, Effort and ASP.NET Boilerplate

9 Comments

Thanks but I want a faster way of using the real deal
That is not a recommended way of testing.You'll in big trouble long run.
@Sampath Why it is not recommended? Because it's slower or? What really are you testing with fake providers - a perfectly running LINQ to Objects queries that throw not supported exceptions at runtime in LINQ to Entities?
@IvanStoev Yes,due to slowness and so many other maintenance related issues where OP is also experiencing right now.I'm using ASP.net Boilerplate for my application development.In memory data testing pattern is default on that framework. aspnetzero.com
"I don't want to use mocks/fakes" then you are by definition 'doing it wrong'
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.