开发者

many-to-many, poco, ef4

开发者 https://www.devze.com 2023-01-31 20:13 出处:网络
I have 3 entities: Goods [GID(PK), GoodName] Persons [PID(PK), PersonName] Roles [RID(PK), RoleName] But now I need to associate these object with each other.

I have 3 entities:

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

But now I need to associate these object with each other. In other words, each Good can have MANY Persons in MANY Roles. I have a table in DB with 3 fields (GID, PID, RID)

For example:

Book (GID#1), can have 3 ass开发者_如何转开发ociated 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)

How can I map this in POCO format in Entity Framework 4?


I believe you have to create another PersonRoles header where you store the Person-Role relation, then you access the person+role via this one:

PersonRoles [PRID(PK), PersonName, RoleName] (note: you could also do this withnow entitykey, just relationships, EF will eliminate this entity and give a direct Person.Roles entity, which you can do access it 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: my main lang is VB.NET so I apologize for the pseudu code above, but I hope you get the idea.

Update

It should be like:

<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

(part from the generated entity by ADO.NET VB POCO Entity Generator)

I just copied the 'Good' Id and nav. property but it should be the three of them.

0

精彩评论

暂无评论...
验证码 换一张
取 消