我正在阅读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);
}
}
Thx inadvance!
答案 0 :(得分:1)
你快到了。尝试将其映射如下。
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<TestA>().HasOptional(a => a.TB)
.WithMany(b => b.TA);
}
关于地图的文章