2010-12-21 6 views
0

J'ai 3 entités:beaucoup à beaucoup, poco, EF4

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

Mais maintenant je dois associer ces objets à l'autre. En d'autres termes, chaque Bien peut avoir BEAUCOUP de Personnes dans BEAUCOUP de Rôles. J'ai une table DB avec 3 champs (GID, PID, RID)

Par exemple:

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) 

Comment puis-je mapper ce format POCO dans Entity Framework 4?

Répondre

0

Je crois que vous devez créer un autre PersonRoles-tête où vous stockez la relation personne-rôle, vous accédez à la personne + rôle par celui-ci:

PersonRoles [PRID(PK), PersonName, RoleName] (note: vous pouvez aussi le faire EntityKey de withnow, juste relations, EF éliminera cette entité et donnera une entité Person.Roles directe, à laquelle vous pouvez accéder via 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); 

Note: mon principal lang est VB.NET donc je présente des excuses pour le code pseudu ci-dessus, mais j'espère que vous avez l'idée.

Mise à jour

Il devrait ressembler à:

<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 

(partie de l'entité générée par ADO.NET VB POCO Entity Generator)

Je viens de copier le 'Good' Id et nav. propriété, mais il devrait être les trois d'entre eux.

+0

Merci pour la réponse rapide, mais j'ai déjà une table en DB avec 3 champs (GID, PID, RID). La question est, comment mapper cette table aux objets dans EF4? – Lari13

+0

Désolé, je n'ai pas eu votre idée :(J'ai déjà une table avec ces relations (GID, PID, RID) .Qu'est-ce que j'ai besoin d'un autre? Ou la réponse ne concerne pas NEW table, mais une nouvelle entité dans EF -Designer? – Lari13

+0

Oui, vous devriez l'avoir aussi dans le modèle – Shimmy

Questions connexes