Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6926213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:53:20+00:00 2026-05-27T10:53:20+00:00

I am trying to separate a large table into multiple discrete types. I’m following

  • 0

I am trying to separate a large table into multiple discrete types.

I’m following the example here: http://weblogs.asp.net/manavi/archive/2011/04/24/associations-in-ef-4-1-code-first-part-4-table-splitting.aspx?CommentPosted=true#commentmessage

It’s working for a primary type and a sub-type, but does not work when I employ multiple types. I received an error

The Entity types ‘CampaginFeedback’ and ‘CampaignSurvey’ cannot share table ‘Campaign’ because they are not int he same type hierarchy
or do not have a valid one to one foreign key relationship with
matching primary keys between them.

Here are simplified versions of my classes:

public class Campaign {
  [Key]
  public int CampaignId {get;set;}
  public string Name {get;set;}
  public virtual CampaignSurvey Survey {get;set;}
  public virtual CampaignFeedback Feedback {get;set;}
}

public class CampaignSurvey {
  [Key]
  public int CampaignId {get;set;}
  public string Question {get;set;}
  public string Answer {get;set;}
}

public class CampaignFeedback {
  [Key]
  public int CampaignId {get;set;}
  public string Feedback {get;set;}
}

protected override void OnModelCreating(DbModelBuilder modelBuilder) {
    modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
modelBuilder.Entity<Campaign>().HasRequired(c => c.Survey).WithRequiredPrincipal();
    modelBuilder.Entity<Campaign>().HasRequired(c => c.Feedback).WithRequiredPrincipal();
    modelBuilder.Entity<Campaign>().ToTable("Campaign");
    modelBuilder.Entity<CampaignSurvey>().ToTable("Campaign");
    modelBuilder.Entity<CampaignFeedback>().ToTable("Campaign");
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T10:53:21+00:00Added an answer on May 27, 2026 at 10:53 am

    Edit: Split table to more than two entities in code first is very problematic. It works without any problem when using EDMX.

    To make it work you must ensure that each entity used to split table has valid one-to-one relation with all other entities used to split table. That also means spoiling your model with navigation properties and moreover ensuring that during save all navigation properties pointing to the same entity type reference the same instance (otherwise you will get exception during call to SaveChanges).

    So the solution for your example should be something like:

    public class Campaign {
      [Key]
      public int CampaignId {get;set;}
      public string Name {get;set;}
      public virtual CampaignSurvey Survey {get;set;}
      public virtual CampaignFeedback Feedback {get;set;}
    }
    
    public class CampaignSurvey {
      [Key]
      public int CampaignId {get;set;}
      public string Question {get;set;}
      public string Answer {get;set;}
    
      public virtual CampaignFeedback Feedback {get;set;}
    }
    
    public class CampaignFeedback {
      [Key]
      public int CampaignId {get;set;}
      public string Feedback {get;set;}
    }
    
    protected override void OnModelCreating(DbModelBuilder modelBuilder) {
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
    
        modelBuilder.Entity<Campaign>().HasRequired(c => c.Survey).WithRequiredPrincipal();
        modelBuilder.Entity<Campaign>().HasRequired(c => c.Feedback).WithRequiredPrincipal();
        modelBuilder.Entity<CampaignSurvey>().HasRequired(c => c.Feedback).WithRequiredPrincipal();
    
        modelBuilder.Entity<Campaign>().ToTable("Campaign");
        modelBuilder.Entity<CampaignSurvey>().ToTable("Campaign");
        modelBuilder.Entity<CampaignFeedback>().ToTable("Campaign");
    }
    

    I’m even not sure how this will work in the real scenario. You can find some other problems when using it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to separate some asp logic out into a separate page. For
I´m trying to separate a gridViewColumn into two rows. Im using default style for
Trying to split this string 主楼怎么走 into separate characters (I need an array) using
I'm trying to write a library to separate all the disk activity out into
I'm trying to abstract out all database code into a separate library and then
What I am trying to accomplish here is to separate a firstname, lastname combo
I am running into problems when trying to draw a large 2D array of
I am trying to use the following query on a table with ~200k records
I am trying to take a rather large CSV file and insert it into
Here's what I'm trying to do. I have an extremely large list of items.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.