操作失败,因为表'TestAs'上已经存在名称为“IX_ID”的索引或统计信息? [英] The operation failed because an index or statistics with name 'IX_ID' already exists on table 'TestAs'?

查看:1457
本文介绍了操作失败,因为表'TestAs'上已经存在名称为“IX_ID”的索引或统计信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using EF4.3.1 code first, I have the code as below,

 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才能修改如下代码,

When I debug the project, the exception is thrown, "The operation failed because an index or statistics with name 'IX_ID' already exists on table 'TestAs'." But, if I remove 'TestC' from the code, only 'TestA' and 'TestB', modify the code as below,

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);

        }
    }
} 

好。任何人知道为什么抛出异常?

everything works well. Anybody know why the exception was thrown?

推荐答案

您正在使用共享主键映射与一个依赖实体和两个主体实体。根据您的maimage主键 TestA 是一个FK到 TestB 以及 TestC 。这就是为什么EF在尝试创建同名的FK时投诉。

You are using the shared primary key mapping with one dependent entity and 2 principle entities. According to your maaping primary key of TestA is a FK to TestB as well as TestC. That is why EF complains when it tries to create 2 FKs with same name.

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

IMO this is not a practical situation. Explain what relationships are among the entities so that situation can be modeled more realistically.

这篇关于操作失败,因为表'TestAs'上已经存在名称为“IX_ID”的索引或统计信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆