所以我和我的团队正在开发一个围绕REST api返回json值的包装器。由于对API的一些调用返回了许多非常一致的值,我们希望将它们缓存在本地数据库中。
目前我们使用TPT数据库映射,但这比TPH慢得多。如果我们切换到TPH虽然EF复制了某些列。这是我们的问题:
预期的TPH映射
+------------------+
| Items |
+------------------+
| Suffix |
+------------------+
| SecondarySuffix |
+------------------+
| ItemType |
+------------------+
EF实际做什么
+------------------+
| Items |
+------------------+
| Suffix |
+------------------+
| SecondarySuffix |
+------------------+
| Suffix1 |
+------------------+
| SecondarySuffix1 |
+------------------+
| Suffix2 |
+------------------+
| SecondarySuffix2 |
+------------------+
| Suffix3 |
+------------------+
| SecondarySuffix3 |
+------------------+
| ItemType |
+------------------+
我的一位程序员认为这是因为4个类实现了相同的接口(IUpgradable
)。因此,由于无法保证第一类的实现与其他三个类的实现兼容,因此它会复制列。
他认为我们最好的机会是覆盖这种行为,但他也注意到这显然是不可能的(或者我们错了)。 这说我们正在考虑添加一个公共基类来避免这个问题。
我们是否错过了至关重要的事情,或者这是TPH方法中的“缺陷”?
//编辑:由于项目是Open sour,我只需链接到文件。我们目前拥有的所有东西都应该在Entities项目中,如果不是Http分支。 https://gw2dotnet.codeplex.com/SourceControl/latest#GW2.NET-Http/GW2.NET.Entities/
导致EF创建重复列的继承树
interface IUpgradable
{
int? Suffix { get; set; }
int? SecondarySuffix { get; set; }
}
abstract class Item
{
int ItemType { get; set; }
}
class Armor : Item, IUpgradable
{
int? Suffix { get; set; }
int? SecondarySuffix { get; set; }
}
class Weapon : Item, IUpgradable
{
int? Suffix { get; set; }
int? SecondarySuffix { get; set; }
}
class Back : Item, IUpgradable
{
int? Suffix { get; set; }
int? SecondarySuffix { get; set; }
}
class Trinket : Item, IUpgradable
{
int? Suffix { get; set; }
int? SecondarySuffix { get; set; }
}
通过引入共同祖先解决问题的继承树
interface IUpgradable
{
int? Suffix { get; set; }
int? SecondarySuffix { get; set; }
}
abstract class Item
{
int ItemType { get; set; }
}
abstract class CombatItem : Item, IUpgradable
{
int? Suffix { get; set; }
int? SecondarySuffix { get; set; }
}
class Armor : CombatItem { }
class Weapon : CombatItem { }
class Back : CombatItem { }
class Trinket : CombatItem { }
问题
对于导致重复列的继承树:我可以强制EF在实现IUpgradable
的所有类中使用相同的列,而不是添加其他级别的继承吗?