2010-12-21 7 views
0

Ich habe 3 Einheiten:many-to-many, poco, EF4

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

Aber jetzt brauche ich diese Aufgabe miteinander zu verknüpfen. Mit anderen Worten, jedes Gute kann VIELE Personen in VIELEN Rollen haben. Ich habe eine Tabelle in DB mit 3 Feldern (GID, PID, RID)

Zum Beispiel:

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) 

Wie kann ich 4 dies in POCO-Format in Entity Framework Karte?

Antwort

0

Ich glaube, Sie einen anderen PersonRoles erstellen haben header, wo Sie die Person-Rollenbeziehung speichern, dann greifen Sie auf die Person + Rolle über diese:

PersonRoles [PRID(PK), PersonName, RoleName] (Anmerkung: Sie können auch diese withnow EntityKey tun, nur Beziehungen wird EF diese Entität eliminieren und eine direkte Entität Person.Roles geben, auf die Sie über Book.Persons.Select((p) p.Roles) zugreifen können.

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); 

Hinweis: mein Haupt lang ist VB.NET so entschuldige ich mich für den pseudu Code oben, aber ich hoffe, Sie bekommen die Idee.

aktualisieren

es sein sollte:

<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 

(Teil aus der erzeugten Einheit von ADO.NET VB POCO Entity Generator)

ich nur kopiert die 'Good' Id und nav. Eigentum, aber es sollte die drei von ihnen sein.

+0

Danke für die schnelle Antwort, aber ich habe bereits eine Tabelle in der DB mit 3 Feldern (GID, PID, RID). Frage ist, wie diese Tabelle Objekten in EF4 zugeordnet wird? – Lari13

+0

Entschuldigung, ich habe deine Idee nicht bekommen :(Ich habe bereits eine Tabelle mit diesen Relationen (GID, PID, RID). Wofür brauche ich eine andere? Oder die Antwort ist nicht über NEW Tabelle, sondern NEUE Entity in EF -Designer? – Lari13

+0

Ja, Sie sollten es auch im Modell haben .. – Shimmy