2016-11-04 153 views
1

我已經有了下面的表格。首先在EF核心1.0上的多對多關係首先與模型?

create table A (Id char(8) primary key, .....) 
create table B (Id int primary key, .....) 
create table A_B (AId references A(Id), BId references B(Id), primarykey(AId, BId)) 

什麼是爲這些多對多關係定義類的最佳方式是什麼?

class A { 
    public string Id { get; set; } 
    .... 
    public List<B> Bs { get; set; } or List<A_B> A_Bs? 
} 
class B { 
    public int Id { get; set; } 
    .... 
    public List<A> As { get; set; } or List<A_B> A_Bs? 
} 
class A_B ??? 

回答

1

EFCore不支持多到許多無連接的表格中,所以你必須使用類似:

class A { 
     List<A_B> A_Bs {get;set;} 
    } 

    class A_B { 
     int AId {get;set;} 
     A MyA {get;set;} 
     int BId {get;set;} 
     B MyB {get;set;} 
    } 

class B { 
    List<A_B> A_Bs {get;set;} 
} 
+0

https://docs.efproject.net/en/latest/modeling /relationships.html#many-to-many –