我是Code First的新手,并尝试构建我的数据模型。 一切都好,除了一个问题,我试着映射下面的东西:
public class Something
{
...
public virtual Layer Layer1 { get; set; }
public virtual Layer Layer2 { get; set; }
public virtual Layer Layer3 { get; set; }
...
}
public class Layer
{
...
public virtual Something Something { get; set; }
...
}
Something类映射确定,但是从Layer到Something的关系根本没有映射,在数据库表中有null,我几乎尝试了所有东西,现在我不知道... 为什么Layer不能引用某些东西?
提前致谢。
答案 0 :(得分:0)
这一起导致:
public class Something {
public int SomethingId { get; set; }
[ForeignKey("Layer1")]
public int Layer1Id { get; set; }
[ForeignKey("Layer2")]
public int Layer2Id { get; set; }
[ForeignKey("Layer3")]
public int Layer3Id { get; set; }
[ForeignKey("Layer1Id")]
public Layer Layer1 { get; set; }
[ForeignKey("Layer2Id")]
public Layer Layer2 { get; set; }
[ForeignKey("Layer3Id")]
public Layer Layer3 { get; set; }
}
public class Layer {
public int LayerId { get; set; }
[InverseProperty("Layer1")]
public Something Something1 { get; set; }
[InverseProperty("Layer2")]
public Something Something2 { get; set; }
[InverseProperty("Layer3")]
public Something Something3 { get; set; }
}