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):
And here is a screen dump of the table I am connecting to:
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?
CODE
The function which uses the EF-connection:
public PrintFirstEmployeeName()
{
try
{
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)
{
Debug.WriteLine(ex.Message);
}
}
The employee model (names are matching database names 1:1):
[Table("dbo.sandbox_employee")]
public class EmployeeModel
{
[Key]
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"?>
<configuration>
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=xxx" requirePermission="false" />
</configSections>
<connectionStrings>
<add name="MyConnString" connectionString="Integrated Security = SSPI; Persist Security Info = False; Data Source = MyServer; Database = MyDatabase" providerName="System.Data.SqlClient"/>
</connectionStrings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7" />
</startup>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
<parameters>
<parameter value="System.Data.SqlClient" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration>
ERRORS
As far as I can tell, there are 3 different errors. All of them are thrown multiple times...
Exception thrown: 'System.Data.SqlClient.SqlException' in System.Data.dll
Exception thrown: 'System.Data.Entity.Core.EntityCommandExecutionException' in EntityFramework.dll
Exception thrown: 'System.Data.Entity.Core.EntityCommandExecutionException' in EntityFramework.SqlServer.dll
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 }!");
}