"EntityConnection can only be constructed with a closed DbConnection" error with dotConnect 4.6.226
Posted: 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:
When executing line  
I' getting exception EntityConnection can only be constructed with a closed DbConnection.
What I'm doing incorrect?
Thx.
			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; }
    }
}Code: Select all
using (DbTransaction transaction = context.Database.Connection.BeginTransaction())What I'm doing incorrect?
Thx.
