How to separate a large table from several discrete types using EF-Code-First

I am trying to separate a large table from several discrete types.

I follow 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 works for the primary type and subtype, but does not work when I use multiple types. I got an error

The CampaginFeedback and CampaignSurvey object types cannot share the Campaign table because they are not a hierarchy of the same type or have no real relationship to the same foreign key 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"); } 
+4
source share
2 answers

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.

+4
source

Something I found works to create a view and point out additional entities to it.

0
source

Source: https://habr.com/ru/post/1386132/


All Articles