我有这些课。
public class Foo
{
public int FooId { get; set; }
public string FooName { get; set; }
}
public class Bar
{
public int BarId { get; set; }
public int FooId { get; set; }
public string BarName { get; set; }
public Foo Foo { get; set; }
}
public class Fox
{
public int FoxId { get; set; }
public int BarId { get; set; }
public string FoxName { get; set; }
public Bar Bar { get; set; }
}
是否有可能确定key
将用作外键的内容?说,在Bar
课堂上,我想将FooId
用作外键分配给Foo
班级,对它也是如此Fox
,将BarId
FK分配给Bar
。
与使用流利的api。它会自动将主键分配为外键。有没有办法覆盖它?
另外,我想问一下这个设计是好是坏。这是一个合理的设计吗?还是我应该为他们创建一个类?
请让我知道你的想法。
编辑
尽可能不要在类中使用数据注释。
如果您想要一对一的关系,您的主键也应该是外键。例如,class中的Bar
FooId
键应该既是主键又是外键。请参阅此链接以获取解释:http : //www.entityframeworktutorial.net/code-first/configure-one-to-one-relationship-in-code-first.aspx
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句