操作失败,因为表'TestAs'上已存在名称为'IX_ID'的索引或统计信息?

时间:2012-03-12 07:08:11

标签: entity-framework ef-code-first code-first one-to-one

我首先使用的是EF4.3.1代码,我的代码如下,

 namespace ConsoleApplication5
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var context = new myContext())
            {
                TestA ta = new TestA();
                ta.Name = "Hero";
                TestB tb = new TestB();
                tb.Name = "Allen";
                TestC tc = new TestC();
                tc.Name = "Iverson";
                ta.tb = tb;
                ta.tc = tc;
                context.testASet.Add(ta);
            }
        }
    }

    class TestA
    {
        public int ID { get; set; }

        public string Name { get; set; }

        public TestB tb { get; set; }

        public TestC tc { get; set; }
    }

    class TestB
    {
        public int ID { get; set; }

        public string Name { get; set; }

        public TestA ta { get; set; }
    }

    class TestC
    {
        public int ID { get; set; }

        public string Name { get; set; }

        public TestA ta { get; set; }
    }

    class myContext : DbContext
    {
        public DbSet<TestA> testASet { get; set; }
        public DbSet<TestB> testBSet { get; set; }
        public DbSet<TestC> testCSet { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<TestB>().HasOptional(x => x.ta).WithRequired();
            modelBuilder.Entity<TestC>().HasOptional(x => x.ta).WithRequired();

            modelBuilder.Entity<TestA>().Property(x => x.ID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
            modelBuilder.Entity<TestB>().Property(x => x.ID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
            modelBuilder.Entity<TestC>().Property(x => x.ID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);


        }
    }
}

当我调试项目时,抛出异常,“操作失败,因为表'TestAs'上已经存在名为'IX_ID'的索引或统计信息。” 但是,如果我从代码中删除'TestC',只删除'TestA'和'TestB',修改下面的代码,

namespace ConsoleApplication5
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var context = new myContext())
            {
                TestA ta = new TestA();
                ta.Name = "Hero";
                TestB tb = new TestB();
                tb.Name = "Allen";               
                ta.tb = tb;
                context.testASet.Add(ta);
            }
        }
    }

    class TestA
    {
        public int ID { get; set; }

        public string Name { get; set; }

        public TestB tb { get; set; }

    }

    class TestB
    {
        public int ID { get; set; }

        public string Name { get; set; }

        public TestA ta { get; set; }
    }


    class myContext : DbContext
    {
        public DbSet<TestA> testASet { get; set; }
        public DbSet<TestB> testBSet { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<TestB>().HasOptional(x => x.ta).WithRequired();

            modelBuilder.Entity<TestA>().Property(x => x.ID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
            modelBuilder.Entity<TestB>().Property(x => x.ID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

        }
    }
} 
一切顺利。有人知道抛出异常的原因吗?

1 个答案:

答案 0 :(得分:-1)

您正在使用与一个依赖实体和两个主要实体的共享主键映射。根据{{​​1}}的maaping主键,FK为TestA以及TestB。这就是EF在尝试创建2个同名的FK时抱怨的原因。

IMO这不是一个实际情况。解释实体之间的关系,以便更现实地建模情况。