EF Code First 抽象关系?
我有一个类继承了另一个类与该基类有关系的类。
示例:
- 基类:动物
- 子类 1:狗 子
- 类 2:猫
- 相关一对多表:疫苗接种
- 一只狗可以接种多次疫苗。这是作为 List
实现的。 - 一只猫可以接种多次疫苗。
- 疫苗接种记录只能与一只动物相关。
- 疫苗接种不知道是否与狗或猫有关。 (狗和猫使用非冲突 GUID。)
- 一只狗可以接种多次疫苗。这是作为 List
没有 Animal 表; Animal 是一个抽象类。但疫苗接种只了解动物,不了解狗。 (然而,EF 知道两者。)我将我的类库分开,这样 Animal 和 Vaccination 位于核心库中,而 Dog 位于引用核心库的另一个库中。
使用 Entity Framework Code First 时,疫苗接种表会获得额外的列:Dog_ID,作为 Dog 类的 List
由于 Animal 是抽象的并且没有数据库表,我可以通过流畅的语句和/或属性/属性声明来完成此操作吗?
I have a class that inherits a base class to which another class has relationships.
Example:
- Base class: Animal
- Subclass 1: Dog
- Subclass 2: Cat
- Related one-to-many table: Vaccinations
- A dog can have multiple vaccinations. This is implemented as a List<Vaccination>.
- A cat can have multiple vaccinations.
- A vaccination record can only have one animal associated with it.
- A vaccination doesn't know if it's associated with a dog or a cat. (Dogs and cats use non-colliding GUIDs.)
There is no Animal table; Animal is an abstract class. But Vaccination knows only about Animal, not about Dog. (EF, however, knows about both.) I have my class libraries split such that Animal and Vaccination are in a core library and Dog is in another library that references the core library.
When using Entity Framework Code First, the Vaccinations table is getting the extra column: Dog_ID, as Dog class's List<Vaccination> explicit declaration is creating an inference. As such, this column maps the vaccination record to the dog. This would be fine, except for the fact that I want to share this extra column across multiple types of Animal. So for example rather than have a Dog_ID and a Cat_ID I'd like to have an Animal_ID that could join to either the Dog or the Cat.
As Animal is abstract and has no DB table, can I accomplish this with perhaps a fluent statement and/or property/attribute declarations?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的
Vaccination
表将始终为每种接种疫苗的动物类型提供额外的 FK 列,除非您创建Animal
表并与该表建立关联。 EF 无法映射和关系的高级抽象 - 关系必须遵循与直接在数据库中创建它们相同的规则。如果只有某些动物可以接种疫苗,您可以向层次结构添加另一个表并与该新表建立关系(EF 无法使用接口)。您将需要 每个类型的表 映射,这将使当前 EF 版本中的查询性能变得更差。
Your
Vaccination
table will always have additional FK column for each vaccinated type of animals unless you createAnimal
table and make relation to that table. EF cannot map and high level abstraction of relations - relations must follow same rules as if you create them directly in the database.If only some animals can be vaccinated you can add another table to hierarchy and make relation with that new table (EF is not able to work with interfaces). You will need Table per Type mapping for that and it will make performance of your queries much worse in current EF version.
假设您的所有动物都接种了疫苗
,那么您可以继承
Cat
和Dog
并使用Table per Type
映射。Assuming all your animals will have vaccinations
Then you can inherit
Cat
andDog
and useTable per Type
mapping.因此,当我实现当前标记为答案的内容时(我最终需要继续创建动物表),我遇到了问题,因为正如我在问题中解释的那样,我有一个独立的项目声明“狗”,并且对于此问题和相关问题,我被指示 这里,此处,以及
So as I implemented what is currently marked as the answer (that I ultimately need to go ahead and create that Animal table) I ran into problems because, as I explained in my question, I have an isolated project declaring the "Dog", and for this and related issues I am directed here, here, and here.