Uncatchable errors from Entity Framework

c# entity-framework entity-framework-6 sql sql-server


I am trying to implement a basic example of Entity Framework, using the Code First approach, with an external database. I just want to read some already existing data, from a table in my database. The way I have implemented it works, in the sense that it shows the right output, but it throws a bunch of exceptions during the process. I have located the line of code, during which the exceptions are thrown, but I cannot catch them, using try/catch (see code).

Here is a screen dump of the Output log (debug) when I run the program (I have highlighted the final output with pink): enter image description here

And here is a screen dump of the table I am connecting to: enter image description here

Even though the output is correct, the errors do seem to affect the performance. It takes somewhere between 2-3 seconds to run the code, even though the table only contains six columns and three rows.

What I have done wrong, to make it behave like this?


The function which uses the EF-connection:

public PrintFirstEmployeeName()
        UniContext db = new UniContext();

        // Exceptions are thrown during this line of code:
        string fullName = db.Employees.FirstOrDefault().first_name 
                  + " " + db.Employees.FirstOrDefault().last_name; 

        Debug.WriteLine($"Hello { fullName }!");
    catch (Exception ex)

The employee model (names are matching database names 1:1):

public class EmployeeModel
    public int employee_id { get; set; }
    public string first_name { get; set; }
    public string last_name { get; set; }
    public int age { get; set; }
    public int company_id { get; set; }

The dbContext implementation:

public class UniContext : DbContext
    public UniContext() : base("Name=MyConnString") { }
    public DbSet<EmployeeModel> Employees { get; set; }

And finally the App.config file:

<?xml version="1.0" encoding="utf-8"?>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=, Culture=neutral, PublicKeyToken=xxx" requirePermission="false" />

    <add name="MyConnString" connectionString="Integrated Security = SSPI; Persist Security Info = False; Data Source = MyServer; Database = MyDatabase" providerName="System.Data.SqlClient"/>

    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7" />


    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">

        <parameter value="System.Data.SqlClient" />


      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />




As far as I can tell, there are 3 different errors. All of them are thrown multiple times...

  1. Exception thrown: 'System.Data.SqlClient.SqlException' in System.Data.dll
  2. Exception thrown: 'System.Data.Entity.Core.EntityCommandExecutionException' in EntityFramework.dll
  3. Exception thrown: 'System.Data.Entity.Core.EntityCommandExecutionException' in EntityFramework.SqlServer.dll
5/7/2018 10:52:41 AM

Accepted Answer

The two tables dbo.__MigrationHistory and dbo.EdmMetadata is (usually) not there for existing databases. Entity Framework (EF) looks for these tables during initialization, which causes the exceptions I saw. Setting the initialization to null right after instantiating the DbContext will tell EF not to look for these tables and no exceptions will occur.

using (UniContext db = new UniContext())
    Database.SetInitializer<UniContext>(null); // Prevents initialization exceptions

    var emp = db.Employees.First();

    Debug.WriteLine($"Hello { emp.FirstName } { emp.LastName }!");
  • See this post for more details.
  • Thanks to @JohnyL for helping me solve the mystery.
5/8/2018 6:37:48 AM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow