开发者

Generic Linq2SQL repository problem with inheritance

开发者 https://www.devze.com 2023-01-28 20:41 出处:网络
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:

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号