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 7657453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:02:14+00:00 2026-05-31T13:02:14+00:00

In my project there are 2 classes (this will increase) Post and Product ,

  • 0

In my project there are 2 classes (this will increase) Post and Product, both can have comments below is the structure i have created but its no working

I have created an interface for entities which can have comments

public interface ICommentable
{
    int Id { get; set; }
}

Post and Product entities may have comments

public class Post : ICommentable
{
    public int Id { get; set; }
    public string Text { get; set; }
    public ICollection<Comment> Comments { get; set; }
}

public class Product : ICommentable
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Rate { get; set; }
    public ICollection<Comment> Comments { get; set; }
}

Comment Entity Itself

public class Comment
{
    public int Id { get; set; }
    public string Message { get; set; }

    public int ItemId { get; set; }
    public ICommentable Item{ get; set; }
}

Data Contxet

public class DataBaseContext:DbContext
{
    public DbSet<Comment> Comments { get; set; }
    public DbSet<Post> Posts { get; set; }
    public DbSet<Product> Products { get; set; }
}
  • 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-31T13:02:15+00:00Added an answer on May 31, 2026 at 1:02 pm

    Entity framework doesn’t support interfaces so it is your first problem. The second problem is that navigation property cannot be normally “polymorphic” = you need separate navigation property (and FK) for every ICommentable you want to relate to your Comment:

    public class Comment
    {
        public int Id { get; set; }
        public string Message { get; set; }
    
        public int PostId { get; set; }
        public Post Post { get; set; }
    
        public int ProductId { get; set; }
        public Product Product { get; set; }
    }
    

    The only scenario where something similar can work is mapped inheritance but it would force your Post and Product to inherit from mapped base entity and this inheritance will be reflected in the database. In such case you can have separate table for base entity and this table can have relation to Comment (instead of derived tables).

    When working with relations always think about the way how would you done it in the database. Your classes mapped by EF must follow similar rules.

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

Sidebar

Related Questions

This is a study project. I have three database classes A,B,C. There is a
I have created a maven android project using this archetype . I want to
I'm working on a C++ project in which there are a lot of classes
Is there any gain to combining all of the classes for a project into
In my project I have the following three interfaces, which are implemented by classes
In my project there are situations where we have to send xml messages (as
I have a project where there are multiple applications that have some common configuration
I have a project which will be made up mostly of Java Servlets, JSP/Html
I have been assigned a project to develop a set of classes that act
I am coding a desktop project which will includes several classes, and will put

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.