Edit: Splitting a table into more than two objects in the code is very problematic at first. It works without any problems when using EDMX.
To make it work, you must make sure that each object used to partition the table has a valid one-to-one relationship with all other objects used to partition the table. It also means messing up your model with navigational properties and, in addition, guaranteeing that while saving all navigational properties that point to the same object type reference, the same instance (otherwise you will get an exception when calling SaveChanges ) .
So, the solution for your example should look something like this:
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 not even sure how this will work in a real scenario. You may find some other problems when using it.
source share