netcore 一对多、一对多 关系

EF Core) 模型的 Entity Framework Core (中的关系使用 (Fk) 的外键来表示。 FK 由关系中依赖实体或子实体上的一个或多个属性组成。

一对一

1
2
3
4
5
6
7
8
9
10
11
12
13
public class A{
[Key]
public int id { get; set; }
public virtual B pk_b { get; set; }
}
public class B{
[Key]
public int id { get; set; }

public int pk_id_a { get; set; }
[ForeignKey("pk_id_a")]
public virtual A pk_a { get; set; }
}

一对多

1
2
3
4
5
6
7
8
9
10
11
12
13
public class A{
[Key]
public int id { get; set; }
public List<B> pk_bs { get; set; }
}
public class B{
[Key]
public int id { get; set; }

public int? pk_id_a { get; set; }
[ForeignKey("pk_id_a")]
public virtual A pk_a { get; set; }
}