使用实体框架根据数据库行中的单元格映射到子类 POCO 对象

发布于 2024-10-13 10:09:33 字数 632 浏览 7 评论 0原文

我正在使用实体框架和 C# POCO 对象。我有一些对象仅在行为上有所不同;它们在数据库中相关的数据行是相同的。数据库中有一行定义包含该行为的类。

我真的很想使用抽象类来实现常见行为,然后根据数据库中的该行实例化实体框架中的子类。这个例子不是我正在研究的,但我认为它是类似的。如果这是处理文档的错误方法,那是因为它只是一个类比,我对文档管理一无所知:)

例如,给出“文档”表中的这两行(带标题行):

DocumentId  Description         DocumentType
314     A word processing document  WordProcessingDocument
315     A spreadsheet           SpreadsheetDocument

理想情况下,我最终会得到一个 Document.WordProcessingDocument 和一个 Document.SpreadsheetDocument,它们具有抽象 Document 类中描述的常见行为。实体框架仍将管理这些对象及其与其他对象的关系。

这可以做到吗?或者实体框架只能为每个表实例化一个类吗?为每个子类型创建一个新表是没有意义的,因为这些表中没有数据。这些子类仅在行为上有所不同,而在数据上没有差异。

I am using Entity Framework and C# POCO objects. I have some objects that differ only in behavior; the rows of data they relate to in the DB are identical. There is a row in the database defining the class that contains the behavior.

I would really like to use an abstract class to implement the common behavior, and then instantiate subclasses from Entity Framework based of that row in the database. This example isn't what I'm working on, but I think it's analogous. If this would be the wrong way to handle a document, it's because it's just an analogy and I don't know anything about document management :)

For example, given these two rows from the "Document" table (with header row):

DocumentId  Description         DocumentType
314     A word processing document  WordProcessingDocument
315     A spreadsheet           SpreadsheetDocument

Ideally, I would end up with one Document.WordProcessingDocument and one Document.SpreadsheetDocument, which would have common behavior described in the abstract Document class. Entity Framework would still be managing these objects and their relationships with other objects.

Can this be done? Or can Entity Framework only instantiate one class per table? It wouldn't make sense to create a new table for each sub-type, since there would be no data in those tables. These subclasses differ in behavior only, not in their data.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

俏︾媚 2024-10-20 10:09:33

这称为每个层次结构的表 (或 TPH)并且在 EF 中是可能的。您必须将基类建模为实体,将具体类建模为从基类派生的实体。您需要一个名为 Discriminator 的特殊列(在您的示例中可能是 DocumentType),它不映射为数据,但定义从数据库加载行时应具体化哪个子类。

This is called Table per Hiearchy (or TPH) and it is possible in EF. You must model your base class as entity and your concrete classes as entities derived from the base class. You need one special column (probably DocumentType in your example) called Discriminator which is not mapped as data but defines which sub class should be materialized when loading row from DB.

囚我心虐我身 2024-10-20 10:09:33

是的,这是一个标准的表继承模式,它使用鉴别器列而不是 1:1 关系。在 EDMX 中创建两个从当前表类型继承的新类型,然后使用映射编辑器指定列 (DocumentType),该列定义要实例化的子类以及与每个子类对应的值。不需要向它们添加任何其他列,但您可以使用分部类来为每个单独的类型定义自定义业务逻辑或行为。

这也意味着 EF 将在插入时自动将这些值分配给行。换句话说,您将实例化 SpreadsheetDocumentWordProcessingDocument,然后只需调用 AddObject。采用此路线将从代码中完全隐藏 DocumentType 列。

Yes, this is a standard table inheritance pattern that uses a discriminator column rather than a 1:1 relation. Create two new types in your EDMX that inherit from the current table type, then use the mapping editor to specify the column (DocumentType) that defines which subclass to instantiate and the value that corresponds to each subclass. There's no requirement that you add any additional columns to them, but you can use partial classes to define custom business logic or behaviors for each individual type.

This also means that EF will automatically take care of assigning these values to the rows upon insert. In other words, you'll instantiate a SpreadsheetDocument or WordProcessingDocument and just call AddObject. Taking this route will hide the DocumentType column from your code entirely.

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