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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:25:37+00:00 2026-06-11T16:25:37+00:00

I have two classes that each implement an interface. One of the classes contains

  • 0

I have two classes that each implement an interface. One of the classes contains an ICollection of the other’s interfaces.

Now I want to map this to my database using EF but get an exception (below). Is this supposed to be possible somehow?

Entity definitions for my classes (products and categories):

public interface IProduct
{
    string ProductId { get; set; }
    string CategoryId { get; set; }
}

public interface ICategory
{
    string CategoryId { get; set; }
    ICollection<IProduct> Products  { get; set; };
}

public class ProductImpl : IProduct
{
    public string ProductId { get; set; }
    public string CategoryId { get; set; }
}

public class CategoryImpl : ICategory
{
    public string CategoryId { get; set; }
    public ICollection<IProduct> Products { get; set; }
}

I want to map the relationship between CategoryImpl and ProductImpl so I’m using the following OnModelCreating method in my DbContext:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    var a = modelBuilder.Entity<CategoryImpl>();
    a.ToTable("Categories");
    a.HasKey(k => k.CategoryId);
    a.Property(p => p.CategoryId);
    a.HasMany(p => p.Products).WithOptional().HasForeignKey(p => p.CategoryId);

    var b = modelBuilder.Entity<ProductImpl>();
    b.ToTable("Products");
    b.HasKey(k => k.ProductId);
    b.Property(p => p.ProductId);
}

The exception I get is below. Am I supposed to somehow specify that the concrete type to be used for IProduct is ProductImpl?

    System.InvalidOperationException: The navigation property 'Products' 
is not a declared property on type 'CategoryImpl'. Verify that it has 
not been explicitly excluded from the model and that it is a valid navigation property.
  • 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-06-11T16:25:39+00:00Added an answer on June 11, 2026 at 4:25 pm

    It’s not possible to do it with interfaces in EF. The type of a navigation property must be mapped for the property to be mapped. And for a type to be mapped it needs to be a concrete type among other things.

    If you need to have different types of products and categories you could instead use a base class for them:

    public class ProductBase
    {
        public string ProductId { get; set; }
        public string CategoryId { get; set; }
    }
    
    public class CategoryBase
    {
        public string CategoryId { get; set; }
        public virtual ICollection<ProductBase> Products { get; set; }
    }
    
    public class DerivedProduct : ProductBase
    {
    }
    
    public class DerivedCategory : CategoryBase
    {
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two classes that refer to each other, but obviously the compiler complains.
If I have two classes that both implement an interface, but also inherit, do
I have a couple of classes that want to pass each other some information
I have two ListActivity classes that I want to pass into into a third
I have two classes SccmAction and TicketAction which both implement interface IDelivery. These classes
I have two classes that I would like to merge into a composite. These
I have two classes that I am testing (let's call them ClassA and ClassB).
I have two classes that represent two different database entities. Their relationship is 1:m
Suppose I have two classes that both contain a static variable, XmlTag. The second
If I have a class called A and I have two classes that derive

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.