System.Data.SqlClient.SqlException: Invalid object name 'dbo.__TransactionHistory'

entity-framework entity-framework-6

Question

I get the error

"System.Data.SqlClient.SqlException: Invalid object name 'dbo.__TransactionHistory'."

when I try creating a new dbcontext using the TransactionContext in EF 6.1.3. It seems like a bug where it's using the transactioncontext API before the DBMigrator/Initializer even has a chance to create it since these operations are done within a transaction.

Below is the code that replicates the problem


using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.SqlServer;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            new SomeDbContext().Database.Initialize(false);
        }

        public class Person
        {
            public virtual int Id { get; set; }
            public virtual string Name { get; set; }
        }

        public class MyConfiguration : DbConfiguration
        {
            public MyConfiguration()
            {
                SetTransactionHandler(SqlProviderServices.ProviderInvariantName, () => new CommitFailureHandler());
            }
        }


        [DbConfigurationType(typeof(MyConfiguration))]
        public class SomeDbContext : DbContext
        {
            static SomeDbContext()
            {
                Database.SetInitializer(new DropCreateDatabaseAlways());
            }

            public SomeDbContext()
                : base(@"Data Source=.;Initial Catalog=SomeDbContext;Integrated Security=True;MultipleActiveResultSets=True;Connect Timeout=1;")
            {
            }

            public virtual DbSet People { get; set; }
        }
    }
}


1
1
9/13/2016 2:58:31 AM

Popular Answer

I ended up doing two things.

  1. Manually run the SQL
     CREATE TABLE [dbo].[__TransactionHistory] (
    [Id] [uniqueidentifier] NOT NULL,
    [CreationTime] [datetime] NOT NULL,
    CONSTRAINT [PK_dbo.__TransactionHistory] PRIMARY KEY ([Id]) 
  2. Creating a CommitFailureHandler that overrides the BeganTransaction to not call the base method until the table has been created
1
9/13/2016 2:34:30 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow