Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

Based on the comments what appears to have happened is:

  • The original EF Upgrade did not work, so you were still running on the old EF
  • When you made the code change, the rebuild triggered that you would use the New EF
  • But the New EF dll's are not available, so you get the error.

Check you config: the EF Version is 6.0 not 6.1, also the Public key token is the same as for .net 2.0.

Just for information on this error

I was configuring Repository and Unit of Work Design Pattern with EF 6.1 with a simple test database named as "School"

It had following 3 tables

BookMaster

Library and

Student

I had created their respective Entity classes, Configurations classes, Repository classes and also created DB Sets in Entities class.

After few days I had created one more table whose above mentioned classes and DbSets were not created.

What I found is providing connectionstring name in base() constructor parameter was giving me following error

'(context.Database.Connection).ServerVersion' threw an exception of type >'System.InvalidOperationException' entity framework 6.1

However if I pass complete connection string as a parameter to base() method then it was working.

After some Trial and Error I deleted the 4th table that was created and It worked like a dream. So may be it is necessary to utilize all the tables and create the above mentioned classes and DbSets which may resolve this 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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top