1

We discovered very strange behavior with Entity Framework last night. It was probably the result of my misguided approach to code first against an existing database, I solved it by turning off the initializer.

Database.SetInitializer<DataContext>(null);

But I would still like to know how such behavior was possible and if it's something we need to be worried about for future EF deployments. Maybe EF code first is not stable across all environments.

We have an existing database that supports the functionality of an existing legacy app. I added new functionality to the app with "code first" and ended up needing to share the same database. So we created the new table with a SQL script and turned on EF Migrations. This all worked fine for me locally, and also worked against the SQL Server instance on my colleague's machine.

However when he tried to run it all from his machine, against the same database, the EF call always timed out with the error:

A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server

We even hard coded a different connection string to see if it would build its own separate database. Same error.

We finally figured out it was the Initializer because while inspecting the DbContext at a break point in the debugger, we triggered the initializer. If we paused execution and expanded the DbContext in a watch window, the database would initialize and the process would continue without error!

It's also worth noting that things were working on his machine until EF noticed model changes. That's when we enabled migrations and updated the table via script and permanently broke the Initializer on his machine.

This is one of the most bizarre things I've experienced. What could cause the EF initializer to hang and throw false network errors about an otherwise functional database...but then work from the debugger? And only on one machine!

grinder22
  • 551
  • 4
  • 17
  • 1
    Where are you calling the initializer? Try calling it from the static constructor on your context. http://stackoverflow.com/questions/16727585/database-setinitializer-to-null-not-working-entity-framework-4-3-1-code-first#16727643 – Steve Greene Apr 09 '16 at 17:08
  • Thanks. Following these links gave me a better understanding of the static nature of the initialization configuration. I think it would be best practice to do any initialization from a static context. I'm going to try turning Migrations back on, and maybe force a Database.Initialize(false) to add some stability. – grinder22 Apr 11 '16 at 16:00
  • We finally did test this by using a static constructor and then calling Initialize(false) explicitly. Same error. It seems our only solution for this machine is to turn off the initializer. – grinder22 Apr 20 '16 at 19:30

0 Answers0