[英]How to define a 0..1 to many relationship in code-first
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. 这些天,当我发现如何创建一对一关系,一对多关系和多对多关系时,我正在阅读ado.net团队博客的文章。 But, is there a way to create 0..1 to many relationship? 但是,有没有一种方法可以创建多个关系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);
}
}
Thx inadvance! 谢谢!
You are almost there. 你快到了。 Try mapping it as follows. 尝试按以下方式映射它。
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<TestA>().HasOptional(a => a.TB)
.WithMany(b => b.TA);
}
Articles on mapping 映射文章
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.