GregL
GregL

Reputation: 77

EF 6 Concrete Object with 2 levels of base classes not working?

Not sure if this is possible EF 6 Code First but I thought I would ask. I am re-factoring some code and adding some new objects to my model which share some common data & functionality.

For Example: Currently I have the following which EF 5 or 6 had no issues with

//Sample Interface
public interface IMyObject {
   string MyName{get;set;}
}

//Sample base class 
public abstract class MyMathBase{

  public int Value1{get;set;}
  public int Value2{get;set;}

  public virtual decimal AddValues(){
    returns Value1+ Value2;
  }

  public virtual decimal MinusValues(){
    returns Value1 - Value2;
  }
}


MyMathObject : MyMathBase,IMyObject { 
   public string MyName{get;set;}

   public MyMathObject(){}
}

Now I have a requirement to add an object for my friend with somewhat the same functionality like so

//Please refer above for interface

//Sample base classes


public abstract class MySubractionBase {
   public int Value1{get;set;)
   public int Value2{get;set;)

  public virtual decimal MinusValues(){
    returns Value1 - Value2;
  }

}

public abstract class MyMathBase : MySubractionBase {


  public virtual decimal AddValues(){
    returns Value1 + Value2;
  }

   //re-factored out the MinusValues method to illustrate how I need to re-factor out some
   //functionality
}



MyMathObject : MyMathBase ,IMyObject { 
   public string MyName{get;set;}


   public MyMathObject (){}
}


//My friend only needs to substract

MyFriendsMathObject : MySubractionBase ,IMyObject { 
   public string MyName{get;set;}

   public MyFriendsMathObject (){}
}

Show I should be able to call MyMathObject.AddValues(1,2) MyMathObject.MinusValues(2,1)

and

MyFriendsMathObject.MinusValues(3,4)

With out having to duplicate the AddValues() method in both objects, it should only be in the objects that require it.

Will EF 6 allow me to map an inheritance tree like this?

Upvotes: 1

Views: 77

Answers (1)

GregL
GregL

Reputation: 77

I got this inheritance structure to work by only mapping the highest base class and concrete objects.

I would love to hear opinions on this design.

Upvotes: 1

Related Questions