多对多,poco,ef4

时间:2010-12-21 13:51:41

标签: entity-framework-4 many-to-many poco

我有3个实体:

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

但现在我需要将这些对象相互关联起来。 换句话说,每个Good可以拥有许多角色中的许多人。 我在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格式映射?

1 个答案:

答案 0 :(得分:0)

我相信你必须创建另一个PersonRoles标题来存储Person-Role关系,然后通过这个访问person +角色:

PersonRoles [PRID(PK), PersonName, RoleName](注意:您也可以使用实体键,只是关系,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);

注意:我的主要语言是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生成的实体)

我刚刚复制了'Good'Id和nav。财产,但应该是他们三个。