我正在将旧的 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)标准设备
我们已经尝试了不同的教程和演练,涉及通过继承模型的每个层次结构表,但没有成功创建可构建的模型。
从技术上讲,我们所做的是:
在这个设置过程中出现了一堆错误,但我们最终得到了这个:
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% 做到这一点,但您可以这样做:
- Create entity OBJECT (I've renamed this 'Vehicle' for the example)
- 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 = '...'
然后使用 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/