"EntityConnection can only be constructed with a closed DbConnection" error with dotConnect 4.6.226

Discussion of open issues, suggestions and bugs regarding ADO.NET provider for SQLite
Post Reply
bairog
Posts: 120
Joined: Mon 29 Apr 2013 09:05

"EntityConnection can only be constructed with a closed DbConnection" error with dotConnect 4.6.226

Post by bairog » Mon 29 Apr 2013 10:14

Hello.
I'm using dotConnect for SQLite 4.6.226 Trial in order to investigate is it suitable for me.
I'm using Entity Framework Code-First (EF version is 5.0).

According to this Devart transactions tutorial and using part of SQLiteConnectionFactory class from CrmDemo.EFCodeFirst.zip project attached to Devart EF Code-First tutorial.

I wrote some code:

Code: Select all

using System;
using System.ComponentModel.DataAnnotations;
using System.Data.Common;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;


namespace EF_CodeFirst_SQLite
{
    class Program
    {
        static void Main(string[] args)
        {
            Database.DefaultConnectionFactory = new SQLiteConnectionFactory();

            var context = new BloggingContext("Data Source=D:\\DBSys.db; FailIfMissing=false;");
            context.Database.Connection.Open();

            using (DbTransaction transaction = context.Database.Connection.BeginTransaction())
            {
                context.Categories.Add(new SystemTypeCategoryDTO() { Id = 1, Name = "" });
                context.SaveChanges();
                transaction.Commit();
            }
        }
    }

    public class SQLiteConnectionFactory : IDbConnectionFactory
    {
        private const string invariantName = "Devart.Data.SQLite";

        public DbConnection CreateConnection(string ConnectionString)
        {
            if (String.IsNullOrEmpty(ConnectionString))
                throw new ArgumentNullException("ConnectionString");

            DbProviderFactory sqliteProviderFactory = DbProviderFactories.GetFactory(invariantName);
            if (sqliteProviderFactory == null)
                throw new InvalidOperationException(String.Format("The '{0}' provider is not registered on the local machine.", invariantName));

            DbConnection connection = sqliteProviderFactory.CreateConnection();
            connection.ConnectionString = ConnectionString;

            return connection;
        }
    }

    public class SystemTypeCategoryDTO
    {
        [Key]
        public int Id { get; set; }
        [Required, MaxLength(50)]
        public String Name { get; set; }
    }

    class BloggingContext : DbContext
    {
        public BloggingContext(string connString)
            : base(connString)
        {
        }

        public DbSet<SystemTypeCategoryDTO> Categories { get; set; }
    }
}
When executing line

Code: Select all

using (DbTransaction transaction = context.Database.Connection.BeginTransaction())
I' getting exception EntityConnection can only be constructed with a closed DbConnection.

What I'm doing incorrect?
Thx.

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Re: "EntityConnection can only be constructed with a closed DbConnection" error with dotConnect 4.6.226

Post by Shalex » Tue 30 Apr 2013 11:34

bairog wrote:I'm using Entity Framework Code-First (EF version is 5.0).
bairog wrote:According to this Devart transactions tutorial ...
bairog wrote:I' getting exception EntityConnection can only be constructed with a closed DbConnection.
The reason of the problem is that our tutorial is about ObjectContext descendant but you are working with DbContext's one. That's why you should open the connection of internal ObjectContext which is within DbContext. Please use

Code: Select all

var connection = ((IObjectContextAdapter)dbContext).ObjectContext.Connection;
connection.Open();
instead of

Code: Select all

var connection = dbContext.Database.Connection;
connection.Open();
using (DbTransaction transaction = context.Database.Connection.BeginTransaction())
It is not necessary to use transaction in your scenario with one SaveChanges() because the transaction is opened implicitly within SaveChanges() if it wasn't opened before on the connection manually.

It makes sense to do if you want to implement several SaveChanges() on the same context and only then to commit the operations made on the connection.

bairog
Posts: 120
Joined: Mon 29 Apr 2013 09:05

Re: "EntityConnection can only be constructed with a closed DbConnection" error with dotConnect 4.6.226

Post by bairog » Fri 03 May 2013 18:43

Shalex wrote:It is not necessary to use transaction in your scenario with one SaveChanges() because the transaction is opened implicitly within SaveChanges() if it wasn't opened before on the connection manually.

It makes sense to do if you want to implement several SaveChanges() on the same context and only then to commit the operations made on the connection.
I've simplyfied my code to paste it to this board, of course I use several SaveChanges() withing that transaction in "real" code :)

Thx for help.

Post Reply