2

I'm going for TPH inheritance here - one Comments table with a discriminator.

I keep getting the error:

The foreign key component 'ResponseId' is not a declared property on type 'PostComment'. Verify that it has not been explicitly excluded from the model and that it is a valid primitive property.

Whenever I try to perform a fresh migration.

Models:

public class Comment : Message {
    [Key]
    [Required]
    public int CommentId { get; set; }
    [Required]
    public int ResponseId { get; set; }
}

public class PostComment : Comment {
    public virtual PostResponse PostResponse { get; set; }
}
public class FlagComment : Comment {
    public virtual FlagResponse FlagResponse { get; set; }
}

Mappings:

public class CommentConfiguration : EntityTypeConfiguration<Comment> {
    public CommentConfiguration() {
        // Comment has Poster (From Message base class)
        HasRequired(c => c.Poster)
        .WithMany()
        .HasForeignKey(u => u.PosterId);
    }
}
public class PostCommentConfiguration : EntityTypeConfiguration<PostComment> {
    public PostCommentConfiguration() {
        // Comment has Response
        HasRequired(c => c.PostResponse)
        .WithMany(s => s.PostComments)
        .HasForeignKey(u => u.ResponseId);
    }
}
public class FlagCommentConfiguration : EntityTypeConfiguration<FlagComment> {
    public FlagCommentConfiguration() {
        // Comment has Response
        HasRequired(c => c.FlagResponse)
        .WithMany(s => s.FlagComments)
        .HasForeignKey(u => u.ResponseId);
    }
}

I even tried adding a new ResponseId to the derived Comment classes just to see if I could budge this forward:

    [Required]
    public new int ResponseId { get; set; }

To no avail - I get the same error. Why is this not working?

RobVious
  • 12,685
  • 25
  • 99
  • 181
  • Bad news: http://stackoverflow.com/q/13953675/861716 – Gert Arnold Dec 07 '13 at 21:37
  • @GertArnold you gotta be kidding me. This has expensive implications... any recommendations? I'm thinking about just tearing out all inheritance and saying &*## it to redundancy. – RobVious Dec 07 '13 at 21:44
  • I've come to the conclusion that having separate tables (for Comments) is the best solution to this when working code first. The hard foreign key constraint is a bonus. – Gert Arnold Dec 07 '13 at 21:49
  • @GertArnold - thanks. I don't mind doing that, but this means I'll have two tables for each Comment type, two tables for each Response type, two ViewModels for all of the above, and then crazy implications on all business logic. I'm so frustrated right now. If you have any time to address a higher level question around this, I'd very much appreciate it: http://stackoverflow.com/questions/20444163/how-to-properly-implement-inheritance-in-ef-with-performance-in-mind – RobVious Dec 07 '13 at 21:52
  • 1
    Well, you can always make the comments and responses implement interfaces, so you can have one view model each and keep the business logic the same. But maybe someone will have a brighter idea, let's wait and see. – Gert Arnold Dec 07 '13 at 21:56

0 Answers0