EF Code First 抽象关系?

发布于 2024-11-18 06:23:00 字数 691 浏览 4 评论 0原文

我有一个类继承了另一个类与该基类有关系的类。

示例:

  • 基类:动物
  • 子类 1:狗 子
  • 类 2:猫
  • 相关一对多表:疫苗接种
    • 一只狗可以接种多次疫苗。这是作为 List实现的。
    • 一只猫可以接种多次疫苗。
    • 疫苗接种记录只能与一只动物相关。
    • 疫苗接种不知道是否与狗或猫有关。 (狗和猫使用非冲突 GUID。)

没有 Animal 表; Animal 是一个抽象类。但疫苗接种只了解动物,不了解狗。 (然而,EF 知道两者。)我将我的类库分开,这样 Animal 和 Vaccination 位于核心库中,而 Dog 位于引用核心库的另一个库中。

使用 Entity Framework Code First 时,疫苗接种表会获得额外的列:Dog_ID,作为 Dog 类的 List。显式声明正在创建推论。因此,该列将疫苗接种记录映射到狗。这很好,除了我想在多种类型的动物之间共享这个额外的列。例如,我不想拥有 Dog_ID 和 Cat_ID,而是希望拥有可以加入 Dog 或 Cat 的 Animal_ID。

由于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

狼性发作 2024-11-25 06:23:00

您的 Vaccination 表将始终为每种接种疫苗的动物类型提供额外的 FK 列,除非您创建 Animal 表并与该表建立关联。 EF 无法映射和关系的高级抽象 - 关系必须遵循与直接在数据库中创建它们相同的规则。

如果只有某些动物可以接种疫苗,您可以向层次结构添加另一个表并与该新表建立关系(EF 无法使用接口)。您将需要 每个类型的表 映射,这将使当前 EF 版本中的查询性能变得更差。

Your Vaccination table will always have additional FK column for each vaccinated type of animals unless you create Animal 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.

初雪 2024-11-25 06:23:00

假设您的所有动物都接种了疫苗

public abstract class Animal
{
   public string ID { get; set; }
   public ICollection<Vaccination> Vaccinations { get; set; }
}

public class Vaccination
{
   public string ID { get; set; }
   public string AnimalID { get; set; }
   public virtual Animal Animal { get; set; }
   //other properties
}

,那么您可以继承 CatDog 并使用 Table per Type 映射。

Assuming all your animals will have vaccinations

public abstract class Animal
{
   public string ID { get; set; }
   public ICollection<Vaccination> Vaccinations { get; set; }
}

public class Vaccination
{
   public string ID { get; set; }
   public string AnimalID { get; set; }
   public virtual Animal Animal { get; set; }
   //other properties
}

Then you can inherit Cat and Dog and use Table per Type mapping.

蓝色星空 2024-11-25 06:23:00

因此,当我实现当前标记为答案的内容时(我最终需要继续创建动物表),我遇到了问题,因为正如我在问题中解释的那样,我有一个独立的项目声明“狗”,并且对于此问题和相关问题,我被指示 这里此处,以及

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文