Organizational Research By

Surprising Reserch Topic

Experts Most Trusted Topic


Question:Access child entities' properties from base entity


More than one year later from my first question on SO (Filter base entity from child entities' properties), I have a similar problem.

I have an abstract base type

public abstract class Base{
}

I have some child entities that inherits from this Base Type

public class Child1 : Base{
    public virtual NavigationProperty NavigationProperty {get; set; }
    public int NavigationPropertyId {get; set}
}

public class Child2 : Base{
    public virtual NavigationProperty NavigationProperty {get; set; }
}

The child entities all have NavigationProperty property. And the NavigationProperty class is like

 public class NavigationProperty{
    public virtual ICollection Child1s {get; set;}
    public virtual Child2 Child2s {get; set;}
 }

There one-to-one mapping between Child2 and NavigationProperty; one-to-many relationship between Child1 and NavigationProperty. In order to these mapping to work i am using TPT. My first question is, can I move

  public NavigationProperty NavigationProperty {get; set; }

to Base class?

I am trying this for a whole day and did not have any success. If it is not possible can i at least access the NavigationProperty from the base type. After all the childs have this property, I tried something like

 public abstract class Base{
      public abstract NavigationProperty NavigationProperty {get; set; }
 }
 ....
 public abstract class Child2{
      public override NavigationProperty NavigationProperty {get; set; }
 }

But entity framework gives the following error.

 Sequence contains more than one matching element

I can use something like

   public abstract class Base{
      public abstract NavigationProperty GetNavigationProperty();
   }

  public abstract class Child2{
      public override NavigationProperty NavigationProperty {get; set; }
      public override NavigationProperty GetNavigationProperty(){
           return NavigationProperty;
      }
 }

But I do not want to introduce these extra methods. Are they any way to achieve this more elegantly?

Edit:

I forgot to mention that I have already tried putting [NotMapped] attribute. I guess EF [NotMapped] attribute is inherited too so the child properties are not mapped too.

I do not expect to Linq-to-Entites to work. I do not want to be able to query base entities with navigation properties. I simply want to get rid of GetNavigationProperty and SetNavigationProperty methods. So when i try to access NavigationProperty from base class it should be loaded into memory, that's all. However, after a week of effort, I do not think it is possible.

asked Sep 13, 2013 in ANDROID by rajesh
edited Sep 12, 2013
0 votes
27 views



Related Hot Questions



Government Jobs Opening


...