5

I'm using Entity Framework 5.0 with code-first approach plus inheritance for my business objects represented by Table Per Hierarchy.

I'd like to have the following structure:

//Assembly 'DataAccess'    
public class MyDbContext : DbContext
{
    DbSet<AbstractClass> CommonObjects.AbstractClasses { get; set; }
}

//Assembly 'CommonObjects'  
public abstract class AbstractClass
{
    //implementation
}

//Assembly 'DerivedObjects'   
public class DerivedClass : AbstractClass
{
    //implementation
}

During runtime, when trying to access the DbContext the first time, the compiler throws an InvalidOperationException saying:

The abstract type 'CommonObjects.AbstractClass' has no mapped descendents and so cannot be mapped. Either remove 'CommonObjects.AbstractClass' from the model or add one or more types deriving from 'CommonObjects.AbstractClass' to the model.

Is this scenario even possible? If yes, what am I doing wrong?

Thanks for your answers in advance.

Ben

Additional information:

Maybe I should be a bit more specific:

I got one assembly containing my abstract business objects (only abstractions). The concrete implementations (containing the logic) are kept in the responsible assemblies, as their logic depends upon other classes within that assembly. The issue is, I want to be able to store those conrete implementations in the persistance layer as well. But for that purpose, EF had to know those types in order to enable the mapping. But I dont want to make the persistance layer depend on my business logic layer - only the abstractions.

That's why I tried to add the derived objects to the DbContext directly from the Business Object Layer.

Example:

AbstractClass derivedClass = new DerivedClass();
MyDbContext.AbstractClasses.Add(derivedClass); 

But then the exception above is being thrown. I just can't figure out a good structure to achieve this.

7
  • Is AbstractClass meant to be a base class for all entities in DerivedObjects? Commented Apr 5, 2013 at 22:46
  • 1
    No, this is just an example. There are several abstract base classes for several derived objects over several assemblies. Commented Apr 6, 2013 at 9:29
  • So you must map the descendants and choose an inheritance strategy. I never tried different assemblies, others did. Commented Apr 6, 2013 at 9:53
  • 1
    Yea. The problem is, if I want to map the descendents, then the assembly with my derived DbContext has to reference the assembly of the descendents. But the descendents shall reference the assembly with the DbContext as well (as they need to have access to the persistance layer). But that would cause a circular dependency. How can I solve that issue? Commented Apr 7, 2013 at 20:13
  • 1
    Thanks for the hint and for your help so far. I have also thought of a similar idea like yours, but due to the requirements this is not applicable. Commented Apr 10, 2013 at 13:33

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.