It was not uncommon to need code like the following to work around the limitations of EF5 and earlier in working with open connections, sharing connections, and sharing transaction. This code works fine on EF5, but now throws on EF6. Note that to repro this first create the database by putting all sets in one DbContext and calling CreateDatabase.
```
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.EntityClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Transactions;
namespace DoubleConnectionTest
{
public class Blog
{
public int Id { get; set; }
public string Title { get; set; }
public virtual ICollection<Post> Posts { get; set; }
}
public class Post
{
public int Id { get; set; }
public string Title { get; set; }
public int BlogId { get; set; }
public virtual Blog Blog { get; set; }
}
public class Junk
{
public int Id { get; set; }
public string Title { get; set; }
}
public class BlogContext : DbContext
{
static BlogContext()
{
Database.SetInitializer<BlogContext>(null);
}
public BlogContext(DbConnection existingConnection)
: base(existingConnection, contextOwnsConnection: false)
{
}
public DbSet<Blog> Blogs { get; set; }
public DbSet<Post> Posts { get; set; }
}
public class JunkContext : DbContext
{
static JunkContext()
{
Database.SetInitializer<JunkContext>(null);
}
public JunkContext(DbConnection existingConnection)
: base(existingConnection, contextOwnsConnection: false)
{
}
public DbSet<Junk> Junks { get; set; }
}
class Program
{
private static void Main(string[] args)
{
using (var transaction = new TransactionScope())
using (var connection = new SqlConnectionFactory().CreateConnection(typeof (BlogContext).FullName))
using (var blogContext = new BlogContext(connection))
using (var junkContext = new JunkContext(connection))
{
var blogOc = ((IObjectContextAdapter) blogContext).ObjectContext;
var junkOc = ((IObjectContextAdapter) junkContext).ObjectContext;
blogOc.Connection.Open();
junkOc.Connection.Open();
var blog = blogContext.Blogs.FirstOrDefault();
var junk = junkContext.Junks.FirstOrDefault();
Console.WriteLine(blog.Title);
Console.WriteLine(junk.Title);
blog.Title = "Modded! + 2";
junk.Title = "Junked! + 2";
blogContext.SaveChanges();
junkContext.SaveChanges();
}
}
}
}
```
Exception is:
```
Unhandled Exception: System.InvalidOperationException: The connection could not be opened because it is already open. Only closed connections can be opened and an already open connection must be closed before it can be opened again.
at System.Data.Entity.Core.EntityClient.EntityConnection.Open()
at DoubleConnectionTest.Program.Main(String[] args) in c:\Stuff\DoubleConnectionTest\DoubleConnectionTest\Program.cs: line 81
```
```
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.EntityClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Transactions;
namespace DoubleConnectionTest
{
public class Blog
{
public int Id { get; set; }
public string Title { get; set; }
public virtual ICollection<Post> Posts { get; set; }
}
public class Post
{
public int Id { get; set; }
public string Title { get; set; }
public int BlogId { get; set; }
public virtual Blog Blog { get; set; }
}
public class Junk
{
public int Id { get; set; }
public string Title { get; set; }
}
public class BlogContext : DbContext
{
static BlogContext()
{
Database.SetInitializer<BlogContext>(null);
}
public BlogContext(DbConnection existingConnection)
: base(existingConnection, contextOwnsConnection: false)
{
}
public DbSet<Blog> Blogs { get; set; }
public DbSet<Post> Posts { get; set; }
}
public class JunkContext : DbContext
{
static JunkContext()
{
Database.SetInitializer<JunkContext>(null);
}
public JunkContext(DbConnection existingConnection)
: base(existingConnection, contextOwnsConnection: false)
{
}
public DbSet<Junk> Junks { get; set; }
}
class Program
{
private static void Main(string[] args)
{
using (var transaction = new TransactionScope())
using (var connection = new SqlConnectionFactory().CreateConnection(typeof (BlogContext).FullName))
using (var blogContext = new BlogContext(connection))
using (var junkContext = new JunkContext(connection))
{
var blogOc = ((IObjectContextAdapter) blogContext).ObjectContext;
var junkOc = ((IObjectContextAdapter) junkContext).ObjectContext;
blogOc.Connection.Open();
junkOc.Connection.Open();
var blog = blogContext.Blogs.FirstOrDefault();
var junk = junkContext.Junks.FirstOrDefault();
Console.WriteLine(blog.Title);
Console.WriteLine(junk.Title);
blog.Title = "Modded! + 2";
junk.Title = "Junked! + 2";
blogContext.SaveChanges();
junkContext.SaveChanges();
}
}
}
}
```
Exception is:
```
Unhandled Exception: System.InvalidOperationException: The connection could not be opened because it is already open. Only closed connections can be opened and an already open connection must be closed before it can be opened again.
at System.Data.Entity.Core.EntityClient.EntityConnection.Open()
at DoubleConnectionTest.Program.Main(String[] args) in c:\Stuff\DoubleConnectionTest\DoubleConnectionTest\Program.cs: line 81
```