[英]Generic Linq2SQL repository problem with inheritance
I have a generic Repository implementation on top of Linq2SQL. 我在Linq2SQL之上有一个通用的Repository实现。 Basically, it uses Table on data context to get the IQueryable and then run the queries over it: 基本上,它在数据上下文上使用Table来获取IQueryable,然后在其上运行查询:
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. System.InvalidOperationException:无法为继承子类型“ Staff”检索表,请尝试使用“人表”。
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 (ie 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. 我可以切换到使用存储库进行查询,但是当涉及到保存对象时,我需要使用派生类型(即Staff),否则将引发另一个异常,因此我最终拥有两个不同的存储库实例:一个用于保存,另一个用于存储。更新操作,另一个用于查询,而一个操作就足够了。
Any help is very much appreciated. 很感谢任何形式的帮助。
我认为这是因为您在Staff
和Resident
类中没有继承Person
类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.