如何在代码优先中定义 0..1 到多个关系
这些天我正在阅读 ado.net 团队博客的文章,当我发现如何创建一对一关系、一对多关系和多对多关系时。但是,有没有办法创建 0..1 到多个关系?
class TestA
{
public int id { get; set; }
public string name { get; set; }
[Timestamp]
public byte[] stamp { get; set; }
public TestB TB { get; set; }
}
class TestB
{
public int id { get; set; }
public string name { get; set; }
[Timestamp]
public byte[] stamp { get; set; }
public ICollection<TestA> TA { get; set; }
}
class myContext : DbContext
{
public DbSet<TestA> players { get; set; }
public DbSet<TestB> teams { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<TestB>().HasOptional<TestA>(a => a);
}
}
提前谢谢!
I'm reading ado.net team blog's articles these days, when I find how to create one to one relationship, one to many relationship and many to many relationship. But, is there a way to create 0..1 to many relationship?
class TestA
{
public int id { get; set; }
public string name { get; set; }
[Timestamp]
public byte[] stamp { get; set; }
public TestB TB { get; set; }
}
class TestB
{
public int id { get; set; }
public string name { get; set; }
[Timestamp]
public byte[] stamp { get; set; }
public ICollection<TestA> TA { get; set; }
}
class myContext : DbContext
{
public DbSet<TestA> players { get; set; }
public DbSet<TestB> teams { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<TestB>().HasOptional<TestA>(a => a);
}
}
Thx inadvance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你快到了。尝试按如下方式映射它。
有关映射的文章
本系列的其他部分也非常好
示例
You are almost there. Try mapping it as follows.
Articles on mapping
Other parts of this series are also very good
Example