1

I have two classes, named Text and Image, which both inherit from the abstract class Item. We've mapped this in a .edmx file and have generated a database from there.

Now, whenever attempting to get a Text or Image from the database, I'm facing the following exception:

There are no EntitySets defined for the specified entity type 'ModelLibrary.Text'.
If 'ModelLibrary.Text' is a derived type, use the base type instead.
Parameter name: TEntity

I can't access or save Item classes, since they are abstract classes and since they never contain the information associated with Text or Image classes.

The piece of code I'm using to try and access the data:

ProjectEntities context = new ProjectEntities(); //this inherits from ObjectContext and was generated by EF

var textRepo = new TextRepository(context);
var lstTexts = textRepo.GetAll();
foreach (var i in lstTexts)
{
    Console.WriteLine(i.textTitle);
}

The textRepo class is used for accessing and saving to the database. I get the exception on the constructor of the class the textRepo inherits from (DataRepository):

protected DataRepository(ObjectContext context)
{
    Context = context;
    ObjectSet = Context.CreateObjectSet<T>();
}

I've already done some research on the exception, and have found this question: EF4 - custom ObjectContext and inheritance question

The answer to the question is actually something that is already supposed to happen (the making of an ObjectSet for the specific type). I just seem to get the exception at the line that is supposed to prevent the exception.

Now, I would like to know what could possibly be going wrong or what I am missing. Do I need to do something extra because of the inheritance? Mind you, I'm a little new with Entity Framework, so my apologies if I'm making a very obvious mistake here. If you need more information or code, feel free to ask.

EDIT: To be more specific on the exception, I get it on the ObjectSet = Context.CreateObjectSet<T>(); line. I can't add much to the piece of testing code, since I already get the exception at the constructor of the baseclass of the textRepo class, namely the DataRepository class.

Community
  • 1
  • 1
Daan
  • 21
  • 4
  • 1
    With regard to the answer you have linked: Did you try the solution proposed there to use `OfType`, i.e. `Context.Items.OfType() as ObjectQuery`? – Slauma Feb 08 '13 at 00:11
  • I haven't, since I didn't really know where that line should be placed. I'll take another look, and maybe risk putting the line all over the place and see where it might work. – Daan Feb 08 '13 at 07:39
  • I have circumvented the entire problem by just ditching the `.edmx` inheritance completely, and replacing it with 0..1 to 1 relations. It's a tad inconvenient, but I've found things elsewhere in my project where dropping the inheritance is somewhat preferable. – Daan Feb 08 '13 at 14:44

0 Answers0