具有基类 DbSet 的每种类型的 EF Code-First 表
我在 DataContext 中有一个用于基类的 DbSet:
public DbSet<BaseClass> Users {get;set;}
我有 2 个派生类客户端和驱动程序。
两者都有一个外键 AdminID。
当我获取特定类型的数据时没有问题。
Users.OfType<Client>.ToList()
但是,如果我尝试同时获取客户端和驱动程序,代码生成中就会出现一些问题:
Users.ToList()
AdminID 属性生成了两次。
AdminID 和 AdminID1。
我认为 sql 生成器会感到困惑,因为我在两个派生类中都有 AdminID,但在父类中没有。
我无法将此属性放入父级中,因为我有另一个没有此属性的派生类。
这是一个错误还是我做错了什么。
I have a DbSet in the DataContext for the base class:
public DbSet<BaseClass> Users {get;set;}
I have 2 derived classes client and driver.
Both have an foreign key AdminID.
When I get data for a specific type there is no problem.
Users.OfType<Client>.ToList()
However if i try to get both Clients and Drivers something gets messed up in the code generation:
Users.ToList()
the AdminID property is generated 2 times.
AdminID and AdminID1.
I think the sql generator gets confused because I have the AdminID in both dervied classes but not in the parent.
I cannot put this property in the parent because I have another derived class that does not have this property.
Is this a bug or am i doing something wrong.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,这不是错误。特点=限制。将属性移动到派生类后,它必须具有到数据库列的唯一映射。因此,您不能在两个派生类之间共享 AdminId。每个类都有自己的外键列和关系。
No it is not bug. It is feature = limitation. Once you move property to derived class it must have unique mapping to database column. Because of that you cannot have shared AdminId between two derived classes. Each class will have its own foreign key column and relation.