2010-12-21 96 views
0

我有3個實體:多到很多,POCO,EF4

Goods [GID(PK), GoodName] 
Persons [PID(PK), PersonName] 
Roles [RID(PK), RoleName] 

但現在我需要將這些對象相互關聯。 換句話說,每個好人可以有許多人擁有許多角色。 我在DB 3個字段的表(GID,PID,RID)

例如:

Book (GID#1), can have 3 associated persons: 

1. Jack (PID#1) in role Author (RID#1) 
2. Jack (PID#1) in role Editor (RID#2) 
3. Bill (PID#2) in role Painter (RID#3) 

我怎樣才能在實體框架4 POCO格式地圖呢?

回答

0

我相信你已經創建另一個PersonRoles頭球您存儲的人,角色關係,那麼你通過這一次訪問的人+角色:

PersonRoles [PRID(PK), PersonName, RoleName](注:你也可以做到這一點withnow的EntityKey,只是EF將會消除這個實體並給出一個直接的Person.Roles實體,你可以通過Book.Persons.Select((p) p.Roles)來訪問它)。

PersonRole#1: Jack#1/Author#1 
PersonRole#2: Jack#1/Editor#2 
PersonRole#3: Bill#2/Painter#3 

Book.PersonRole = context.PersonRoles. 
    SingleOrDefault((pr) => pr.Person.PersonId == 1 && pr.RoleId == 1); 

注:我的主要lang是VB.NET,所以我對上面的pseudu代碼道歉,但我希望你的想法。

更新

應該是這樣:

<DataContract(IsReference:=True)> 
<KnownType(GetType(Good))> 
<KnownType(GetType(Person))> 
<KnownType(GetType(Role))> 
Partial Public Class GoodPersonRole 
    Implements IObjectWithChangeTracker 
    Implements INotifyPropertyChanged 

<DataMember()> 
Public Property GoodId() As Integer 
    Get 
     Return _goodId 
    End Get 
    Set(ByVal value As Integer) 
     If Not Equals(_goodId, value) Then 
      If ChangeTracker.ChangeTrackingEnabled AndAlso ChangeTracker.State <> ObjectState.Added Then 
       Throw New InvalidOperationException("The property 'GoodId' is part of the object's key and cannot be changed. Changes to key properties can only be made when the object is not being tracked or is in the Added state.") 
      End If 
      If Not IsDeserializing Then 
       If Good IsNot Nothing AndAlso Not Equals(Good.GoodId, value) Then 
        Good = Nothing 
       End If 
      End If 
      _goodId = value 
      OnPropertyChanged("GoodId") 
     End If 
    End Set 
End Property 

Private _goodId As Integer 


<DataMember()> 
Public Property Good() As Good 
    Get 
     Return _good 
    End Get 
    Set(ByVal value As Good) 
     If _good IsNot value Then 
      If ChangeTracker.ChangeTrackingEnabled AndAlso ChangeTracker.State <> ObjectState.Added AndAlso value IsNot Nothing Then 
       ' This the dependent end of an identifying relationship, so the principal end cannot be changed if it is already set, 
       ' otherwise it can only be set to an entity with a primary key that is the same value as the dependent's foreign key. 
       If Not Equals(GoodId, value.GoodId) Then 
        Throw New InvalidOperationException("The principal end of an identifying relationship can only be changed when the dependent end is in the Added state.") 
       End If 
      End If 
      Dim previousValue As Good = _good 
      _good = value 
      FixupGood(previousValue) 
      OnNavigationPropertyChanged("Good") 
     End If 
    End Set 
End Property 
Private _good As Good 
End Class 

(由ADO.NET VB POCO Entity Generator部分從生成的實體)

我剛纔複製的 '好' Id和導航。財產,但它應該是其中的三個。

+0

感謝您的快速回答,但我已經在DB有3個字段(GID,PID,RID)的表。問題是,如何將此表映射到EF4中的對象? – Lari13 2010-12-21 14:04:16