Generic Linq2SQL repository problem with inheritance
I have a generic Repository implementati开发者_JAVA百科on on top of Linq2SQL. Basically, it uses Table on data context to get the IQueryable and then run the queries over it:
private readonly IQueryable _queryable;
private readonly DataContext _context;
public Repository(DataContext context)
{
_context = context;
_queryable = context.GetTable().AsQueryable();
}
The problem is with inheritance. If I have two inherited tables, an exception is thrown:
[InheritanceMapping(Code="1", Type=typeof(Staff))]
[InheritanceMapping(Code="2", Type=typeof(Resident), IsDefault=true)]
public class Person { }
public class Resident : Person { }
public class Staff : Person { }
now when creating Repository an exception is thrown:
System.InvalidOperationException: Could not retrieve a Table for inheritance subtype 'Staff', try Table of Person instead.
Now the story does not end here. I CAN switch to using Repository for querying, but when it comes down to saving objects, I need to use the derived type (i.e. Staff) or another exception will be thrown, so I end up having two different repository instances: one for save and update operations and the other for querying, while one should be enough.
Any help is very much appreciated.
I think that is because you classes Staff
and Resident
do not inherit from class Person
.
精彩评论