System.Data.SqlClient.SqlException:无效的对象名称' dbo .__ TransactionHistory'

时间:2016-09-13 01:35:21

标签: entity-framework entity-framework-6

我收到错误

  

" System.Data.SqlClient.SqlException:无效的对象名称   ' DBO .__ TransactionHistory'"

当我尝试使用EF 6.1.3中的new dbcontext创建TransactionContext时。在DBMigrator/Initializer甚至有机会创建它之前,它似乎是一个使用transactioncontext API的错误,因为这些操作是在事务中完成的。

以下是复制问题的代码


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 个答案:

答案 0 :(得分:1)

我最后做了两件事。

  1. 手动运行SQL
     CREATE TABLE [dbo].[__TransactionHistory] (
    [Id] [uniqueidentifier] NOT NULL,
    [CreationTime] [datetime] NOT NULL,
    CONSTRAINT [PK_dbo.__TransactionHistory] PRIMARY KEY ([Id]) 
  2. 创建一个CommitFailureHandler,它覆盖BeganTransaction,以便在创建表之前不调用基本方法