none
Ef table splitting RRS feed

  • Question

  • Hi! Could somebody explain why
    class TestA
    {
        public int TestAId { get; set; }
        public string A { get; set; }
        public TestB TestB { get; set; }
    }
    
    class TestB
    {
        public int TestBId { get; set; }
        public string B { get; set; }
        public TestA TestA { get; set; }
    }
    
    class TestContext : DbContext
    {
        public DbSet<TestA> TestAs { get; set; }
    
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<TestB>().Property(t => t.TestBId).HasColumnName("TestAId");
            modelBuilder.Entity<TestB>().HasRequired(t => t.TestA).WithRequiredDependent(t => t.TestB);
            modelBuilder.Entity<TestB>().ToTable("Test");
            modelBuilder.Entity<TestA>().ToTable("Test");
        }
    }

    works great. But if i try to make TestB principal

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<TestB>().Property(t => t.TestBId).HasColumnName("TestAId");
        modelBuilder.Entity<TestB>().HasRequired(t => t.TestA).WithRequiredPrincipal(t => t.TestB);
        modelBuilder.Entity<TestB>().ToTable("Test");
        modelBuilder.Entity<TestA>().ToTable("Test");
    }

    it fails with exception: 

    Multiple identity columns specified for table 'Test'. Only one identity column per table is allowed. 

    Thanks in advance!


    Saturday, February 22, 2014 8:36 AM

Answers

  • Hello sys13,

    WithRequiredDependent, it will create one identity column in the generated table. You can check the generated table, it should be:

    [TestAId] INT            IDENTITY (1, 1) NOT NULL,

    WithRequiredPrincipal, it is trying to create two identified columns like below:

    [TestAId] INT            IDENTITY (1, 1) NOT NULL,
    
    [TestBId] INT            IDENTITY (1, 1) NOT NULL,
    

    However,We can't have two Identity columns.

    Regards.

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, February 24, 2014 3:11 AM
    Moderator