I have a class library I have been using for over a year that has recently stopped working after upgrading to EF 6.1.
I have tried various methods for passing in the connect string to my Context class constructor but while it seems to correctly pass the string I invariably receive:
'(((System.Data.Entity.DbContext)(context)).Database.Connection).ServerVersion' threw an exception of type 'System.InvalidOperationException'
and the connection state stays closed.
Here is my AppConfig:
<?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=b77a5c561934e089" requirePermission="false" />
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --></configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />
</startup>
<connectionStrings>
<add name="MyContext" connectionString="Data Source=Server;Initial Catalog=DBName;Integrated Security=True;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />
</connectionStrings>
<entityFramework>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration>
My test class:
using System.Data.Entity;
namespace SVMIC.IMS.Entity.IMSClaim
{
public class Context:DbContext
{
static Context()
{
Database.SetInitializer<Context>(null);
}
public Context():base("MyContext")
{
}
}
}
and my test app:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using SVMIC.IMS.Entity.IMSClaim;
namespace TestApp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Context context = new Context();
}
}
}
The database server is SQL Server 2008 R2.
I assume it is something simple changed in 6.1 and I am just missing it, but I am totally stumped.
Ok, I have resolved the issue.
In frustration at not being able to fix the issue with 6.1, nothing I did made any difference to the result, and even building and stepping through the EF source accomplished little so I rolled back to EF 5.0 and rebuilt my code.
EF 5 immediately returned an error message identifying an issue in the schema definition, a view added by another developer that referenced system views was confusing my template, I fixed the issue and was back working in a matter of minutes.
The lesson learned here was, EF 6 needs some serious work on its error handling, as just returning a generic exception error left me chasing off in every direction, while the error message from EF 5 pointed me directly at the issue.
If you are working local MDF file, the update the folder path of MDF file in App.config/ Web.config else copy the dbmx App.cofig connectionStrings and paste to your project App.config/Web.config file.