我有一个实体(A),它在自己的表中存储数据。我想将此实体作为另一个实体(B)的属性,但将其所有数据存储在实体B的表中(有点像复杂类型),因此我可以在那时编写该实体的确切值。
我已经尝试过自己做,但是实体B始终将实体A视为外键,即使该属性不是虚拟的,我也尝试了继承工作,但不能正常工作。我可以精确复制实体A的类型并将其分配给它来解决它,但我是DRY的忠实粉丝,觉得它只会增加不必要的复杂性。
提前致谢
编辑:下面的代码,我无法发布原始代码,但这是它的外观
public class EntityA {
[Key]
public int id { get; set; }
public int prop1 { get; set; }
public virtual AnotherEntity { get; set; }
}
public class EntityB {
// foreign key as part of composite key
// entity C code not included as doesn't
// affect the problem
[Key, Column(Order=0)]
public int EntityCId { get; set; }
public virtual EntityC { get; set; }
// this is set as part of the composite key
// as I want to use entity ID but
// not make the field foreign key
[Key, Column(Order=1)]
public EntityA saveInToEntityBTable { get; set; }
}
解决它之后,您要做的就是抽象一个可以继承的基类。由于基类是抽象的,因此它将永远不会创建表,因此继承的类没有可链接的外键。
public class EntityA : EntityABase {
}
public abstract class EntityABase {
[Key]
public int id { get; set; }
public int prop1 { get; set; }
public virtual AnotherEntity { get; set; }
}
public class EntityB : EntityABase {
[Key]
public virtual EntityC { get; set; }
}
注意:如果您不想使用抽象类,则可以使用TPC(每种具体类型的表),但是我在迁移时遇到了问题,即没有在EntityB上为属性“ AnotherEntity”创建列或外键(实体A很好地创建了它们) )
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句