Friday, September 2, 2016

Improving Entity Framework Performance during testing

Leave a Comment

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.

2 Answers

Answers 1

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!

Answers 2

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

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment