我试图弄清楚这里发生了什么。当我为数据库设定种子时,它生成的外键始终为空。我的模型很简单:
public class PnlTarget
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int PnlTargetId { get; set; }
public ReferenceAsset Underlyer { get; set; }
public decimal Target { get; set; }
public virtual PnlSettings PnlSettings { get; set; }
}
public class PnlSettings
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
public int PnlSettingsId { get; set; }
public ColloquialType Type { get; set; }
public virtual PnlTarget SPTSX60 { get; set; }
public virtual PnlTarget SPTXLVPR { get; set; }
public virtual PnlTarget STBANKX { get; set; }
public virtual PnlTarget SX5E { get; set; }
public virtual PnlTarget RTY { get; set; }
public virtual PnlTarget SPX { get; set; }
[DataType(DataType.DateTime)]
public DateTime LastEdited { get; set; }
public string LastEditedBy { get; set; }
}
所以它是一个1到0到很多的关系。当我用这样的东西播种时:
PnlSettings vanillaAccelerator = new PnlSettings
{
PnlSettingsId = 1,
Type = ColloquialType.VanillaAccelerator,
RTY = new PnlTarget {Underlyer = ReferenceAsset.RTY, Target = (decimal) 0.004},
SPX = new PnlTarget {Underlyer = ReferenceAsset.SPX, Target = (decimal) 0.004},
SPTSX60 = new PnlTarget { Underlyer = ReferenceAsset.SPTSX60, Target = (decimal)0.004},
SPTXLVPR = new PnlTarget { Underlyer = ReferenceAsset.SPTXLVPR, Target = (decimal)0.004},
STBANKX = new PnlTarget { Underlyer = ReferenceAsset.STBANKX, Target = (decimal)0.004},
SX5E = new PnlTarget { Underlyer = ReferenceAsset.SX5E, Target = (decimal)0.004},
LastEdited = DateTime.Now,
LastEditedBy = "SEED"
};
我的外键属性始终为null,如此处所示。我尝试使用[ForeignKey]注释手动实现外键属性,但不幸的是它仍然生成了这个空列。这是为了吗?
谢谢!