每个层次结构表和继承实现问题

发布于 2024-09-01 13:56:59 字数 1105 浏览 6 评论 0原文

我正在将旧的 ASP 应用程序迁移到现代 .NET 版本,为了减少开发时间,我们正在考虑 .NET 4.0 实体框架。然而,我们在开发过程中似乎遇到了这个问题。

给出的是我们数据库的一小部分: 一个表 OBJECT,其中包含汽车及其各自属性的列表。我们还有一个表 OBJECT_OPTIONS,其中包含 OBJECT 中给定汽车的选项、配件和标准设备的列表。这三种类型都具有相同的字段,因此存储在同一个表中。 ncopt_type 列用于区分不同的列表。可能的值为:“opt”、“acc”和“sta”。表 OBJECT_OPTIONS 通过 ncopt_obj_id 链接到 OBJECT,它表示表 OBJECT 中唯一的汽车 (obj_id)。

我们的目标是为 OBJECT 实体提供 3 个链接到不同 OBJECT_OPTIONS 列表的属性: - 财产选择 - 财产配件 - 属性标准设备

我们已经通过继承模型尝试了有关每个层次结构表的不同教程和演练,但尚未成功创建可构建的模型。

从技术上讲,我们所做的是:

  • 创建实体 OBJECT
  • 创建实体 OBJECT_OPTIONS,使其抽象
  • 添加实体 OPTION、ACCESSORY 和 STANDARD_EQUIP 全部使用基本类型 OBJECT_OPTIONS
  • 向 ncopt_type = '...' 上的所有三个表添加条件
  • 向 OBJECT 添加 3 个导航属性,全部链接到继承的实体之一:选项、配件和标准设备

在此设置过程中会出现一堆错误,但我们最终得到的是这个:

错误 3032:从第 250、286 行开始的映射片段出现问题:EntityTypes NCO.Model.OPTION、NCO.Model.ACCESSOIRE、NCO.Model.STANDAARD_EQUIP 正在映射到表 OBJECT_OPTIES 中的相同行。映射条件可用于区分这些类型映射到的行。

但所有三个物体都存在一个条件。

我还没有找到解决这个问题的方法,并且已经在上面花费了太多时间。我们目前正在使用一种解决方法,但希望解决这个问题,因为这种情况在项目结束时会多次出现。

感谢任何帮助,如果您需要更多信息,请给我留言或发送电子邮件。

I am migrating an old ASP application to a modern .NET version, to cut down on development times we are looking at the .NET 4.0 Entity Framework. However, we seem to have hit a brick wall in our development with this issue.

Given is a small part of our database:
A table OBJECT which contains a list of cars and their respective properties. We also have a table OBJECT_OPTIONS which contains, for a given car in OBJECT, a list of OPTIONS, ACCESSORIES and STANDARD EQUIPMENT. These three types all have the same fields and are therefor stored in the same table. The column ncopt_type is used to discriminate between the different lists. Possible values are: 'opt', 'acc' and 'sta'. The table OBJECT_OPTIONS links to OBJECT via ncopt_obj_id which represents a unique car (obj_id) in table OBJECT.

Our goal is to provide the OBJECT entity with 3 properties that link to the different OBJECT_OPTIONS lists:
- property OPTIONS
- property ACCESSORIES
- property STANDARDEQUIPMENT

We've tried different tutorials and walkthroughs concerning the table-per-hierarchy via inheritance model but haven't succeeded in creating a buildable model.

Technically what we did was:

  • Create entity OBJECT
  • Create entity OBJECT_OPTIONS, make it abstract
  • Add entities OPTION, ACCESSORY and STANDARD_EQUIP all using basetype OBJECT_OPTIONS
  • Add conditions to all three tables on ncopt_type = '...'
  • Add 3 navigational properties to OBJECT, all linking to one of the inherited entities: OPTIONS, ACCESSORIES and STANDAARD_EQUIPMENT

A bunch of errors shows up during this setup, but we end up with this one:

Error 3032: Problem in mapping fragments starting at lines 250, 286:EntityTypes NCO.Model.OPTION, NCO.Model.ACCESSOIRE, NCO.Model.STANDAARD_EQUIP are being mapped to the same rows in table OBJECT_OPTIES. Mapping conditions can be used to distinguish the rows that these types are mapped to.

There is a condition present on all three objects though.

I have found no solution to this problem and have spent way too much time on it already. We are currently using a workaround method but would love to get this fixed as this situation will present itself a few more times by the end of the project.

Any help appreciated, if you need more information, please drop me a comment or an email.

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

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

发布评论

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

评论(1

[旋木] 2024-09-08 13:57:07

我不相信您可以在 EF 设计器中 100% 地完成此操作,但您可以这样做:

  1. 创建实体对象(我已将此示例重命名为“车辆”)
  2. 创建实体 OBJECT_OPTIONS,使其抽象
  3. 使用基本类型添加实体 OPTION、ACCESSORY 和 STANDARD_EQUIP
    OBJECT_OPTIONS
  4. 向 ncopt_type = '...' 的所有三个表添加条件

然后使用车辆的部分类添加所需的属性:

using System.Collections.Generic;
using System.Linq;

public partial class Vehicle
{
    public IEnumerable<ACCESSORY> Accessories
    {
        get { return this.OBJECT_OPTIONS.OfType<ACCESSORY>(); }
    }

    public IEnumerable<OPTION> Options
    {
        get { return this.OBJECT_OPTIONS.OfType<OPTION>(); }
    }

    public IEnumerable<STANDARD_EQUIP> StandardEquipments
    {
        get { return this.OBJECT_OPTIONS.OfType<STANDARD_EQUIP>(); }
    }
}

I'm not convinced you can do this 100% from within the EF designer, but you can do it like this:

  1. Create entity OBJECT (I've renamed this 'Vehicle' for the example)
  2. Create entity OBJECT_OPTIONS, make it abstract
  3. Add entities OPTION, ACCESSORY and STANDARD_EQUIP all using basetype
    OBJECT_OPTIONS
  4. Add conditions to all three tables on ncopt_type = '...'

Then add in the properties you want using a partial class for Vehicle:

using System.Collections.Generic;
using System.Linq;

public partial class Vehicle
{
    public IEnumerable<ACCESSORY> Accessories
    {
        get { return this.OBJECT_OPTIONS.OfType<ACCESSORY>(); }
    }

    public IEnumerable<OPTION> Options
    {
        get { return this.OBJECT_OPTIONS.OfType<OPTION>(); }
    }

    public IEnumerable<STANDARD_EQUIP> StandardEquipments
    {
        get { return this.OBJECT_OPTIONS.OfType<STANDARD_EQUIP>(); }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文