.net - Table-per-hierarchy 和继承实现问题

标签 .net entity-framework-4 table-per-hierarchy

我正在将旧的 ASP 应用程序迁移到现代 .NET 版本,以减少我们正在研究 .NET 4.0 Entity Framework 的开发时间。但是,我们似乎在开发过程中遇到了这个问题。

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

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

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

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

  • 创建实体 OBJECT
  • 创建实体 OBJECT_OPTIONS,使其抽象
  • 添加实体 OPTION、ACCESSORY 和 STANDARD_EQUIP 都使用基本类型 OBJECT_OPTIONS
  • 向所有三个表添加条件 ncopt_type = '...'
  • 向 OBJECT 添加 3 个导航属性,所有这些属性都链接到继承的实体之一:OPTIONS、ACCESSORIES 和 STANDAARD_EQUIPMENT

  • 在这个设置过程中出现了一堆错误,但我们最终得到了这个:

    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.



    但是,所有三个对象都存在条件。

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

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

    最佳答案

    我不相信您可以在 EF 设计器中 100% 做到这一点,但您可以这样做:

    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 = '...'


    然后使用 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>(); }
        }
    }
    

    关于.net - Table-per-hierarchy 和继承实现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2856170/

    相关文章:

    .net - 为什么 F# 的 printfn 可以处理文字字符串,但不能处理字符串类型的值?

    c# - 内部 .NET Framework 数据提供程序错误 1025

    sql-server - Microsoft SQL Server 2008 - 非聚集、非唯一索引上有 99% 的碎片

    c# - 在 dapper 中查询抽象模型

    entity-framework - TPT 之上的 Entity Framework TPH

    c# - Entity Framework :- Error when Casting to derive class throw exception in Table per hierarchy query

    .net - wsHTTPBinding 身份验证错误

    java - Java 与 .NET Func<> 和 Action<> 委托(delegate)最接近的是什么?

    c# - 为什么 List<T>、Dictionary<T> 和其他集合包含一个名为 "version"的字段?

    c# - .NET 4.0 Entity Framework 超时已过期