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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T06:36:01+00:00 2026-06-08T06:36:01+00:00

I have the following class definitions public abstract class AbstractClass { [Key] public string

  • 0

I have the following class definitions

public abstract class AbstractClass
{

    [Key]
    public string Name { get; set; }
    public virtual IndependentClass IndependentClass { get; set; }

    public string IndependentClassName { get { return IndependentClass == null ? "<NULL>" : IndependentClass.Name; } }
}

public class Impl1 : AbstractClass
{
}

public class Impl2 : AbstractClass
{
}

public class IndependentClass
{
    [Key]
    public string Name { get; set; }

    public virtual Impl1 Impl1 { get; set; }
    public virtual ICollection<Impl2> Impl2s { get; set; }
}

with these in my Context.OnModelCreating:

modelBuilder.Entity<AbstractClass>().HasOptional(abs => abs.IndependentClass);
modelBuilder.Entity<IndependentClass>().HasRequired(ind => ind.Impl1);
modelBuilder.Entity<IndependentClass>().HasMany(ind => ind.Impl2s);

My initialization looks like this (with adding the arrays to the context and context.SaveChanges() trimmed)

var impl1s = new[]
{
    new Impl1() { Name = "a" },
    new Impl1() { Name = "b" },
    new Impl1() { Name = "c" }
}

var inds = new[]
{
    new IndependentClass() { Name = "A", Impl1 = impl1s[0] },
    new IndependentClass() { Name = "B", Impl1 = impl1s[1] }
}

var impl2s = new[]
{
    new Impl2() { Name = "a1", IndependentClass = inds[0] },
    new Impl2() { Name = "a2", IndependentClass = inds[0] },
    new Impl2() { Name = "b1", IndependentClass = inds[1] },
    new Impl2() { Name = "b2", IndependentClass = inds[1] },
    new Impl2() { Name = "c1", IndependentClass = null }
}

And finally I have a simple View that dumps each of the db collections. My problem is that the navigation properties aren’t getting populated like I need them to. i.e., the output of my view is this:

Independent Classes:

    A: Impl1 = a, Impl2s = []
    B: Impl1 = b, Impl2s = []

Impl1s

    a: IndependentClass = <NULL>
    b: IndependentClass = <NULL>
    c: IndependentClass = <NULL>

Impl2s

    a1: IndependentClass = A
    a2: IndependentClass = A
    b1: IndependentClass = B
    b2: IndependentClass = B
    c1: IndependentClass = <NULL>

The IndepdendentClasses should have their Impl2s populated, and the first two Impl1s should have IndependentClasses populated.

Can anyone tell me what I’m doing wrong?

  • 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-08T06:36:03+00:00Added an answer on June 8, 2026 at 6:36 am

    Your model has three relationships with the following navigation properties:

    • AbstractClass.IndependentClass <-> IndependentClass.(NoNavigationProperty)
    • IndependentClass.Impl1 <-> Impl1.(NoNavigationProperty)
    • IndependentClass.Impl2s <-> Impl2.(NoNavigationProperty)

    For each relationship one end of the association is not exposed as navigation property. It’s not expected in this model that…

    The IndepdendentClasses should have their Impl2s populated, and the
    first two Impl1s should have IndependentClasses populated

    …because you don’t populate IndependentClass.Impl2s in your initialization code nor do you fill Impl1.IndependentClass. You fill Impl2.IndependentClass but because this navigation property belongs to another relationship it doesn’t affect IndependentClass.Impl2s at all.

    If you want actually two relationships…

    • IndependentClass.Impl1 <-> Impl1.IndependentClass (one-to-one)
    • IndependentClass.Impl2s <-> Impl2.IndependentClass (one-to-many)

    …you can’t achieve that as long as your AbstractClass is an entity in your model with its own table because you must map between navigation properties of the types they are declared in, and Impl1.IndependentClass and Impl2.IndependentClass are inherited properties but they are not declared in Impl1 and Impl2.

    If you don’t make AbstractClass an entity, i.e. don’t use this abstract class in mapping code and don’t have a DbSet<AbstractClass> in your context class, then the two relationships above are possible. For EF your model does not have any inheritance, instead it considers Impl1 (and Impl2) as an entity that just contains its own properties plus the properties of the base class as if it were one class without base.

    That’s the reason why the mapping in your own answer works as you expect: You have defined the one-to-one relationship between IndependentClass.Impl1 and Impl1.IndependentClass explicitly with Fluent API. The second relationship between IndependentClass.Impl2s and Impl2.IndependentClass is detected automatically by naming conventions as one-to-many relationship.

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

Sidebar

Related Questions

Given the following class definitions: public class BaseClass { public string SomeProp1 { get;
Well, I have the following class definitions: private ObservableCollection<Node> _Nodes; public ObservableCollection<Node> Nodes {
I have the following definitions: public class BaseEntity { ... public BaseEntity() { }
I have a question about .net generics. Consider the following code: public abstract class
I have the following abstract classes: public abstract class AbSuperClass1<K,S> { //class definition }
I have the following base abstract class defined as: public abstract class BaseObject<T> :
I have this Base class: abstract class Base { public int x { get
I have a class with the following definition: public abstract class A<T> implements Iterator<B>
I have the following class definition: public interface IItem{} public class FirstObject<T, U> :
I have the following property definition: public static class Extensions { public static readonly

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.