【问题标题】:many-to-many, poco, ef4多对多、poco、ef4
【发布时间】:2010-12-21 22:28:16
【问题描述】:

我有 3 个实体:

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)

如何在 Entity Framework 4 中将其映射为 POCO 格式?

【问题讨论】:

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


    【解决方案1】:

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

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

    注意:我的主要语言是 VB.NET,所以我为上面的伪代码道歉,但我希望你明白。

    更新

    应该是这样的:

    <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 和导航。属性,但应该是他们三个。

    【讨论】:

    • 感谢您的快速回答,但我已经在 DB 中有一个包含 3 个字段(GID、PID、RID)的表。问题是,如何将此表映射到 EF4 中的对象?
    • 对不起,我不明白你的想法 :( 我已经有一张包含这些关系(GID、PID、RID)的表。我需要另一个有什么用?或者答案不是关于新的表,但 EF-Designer 中的新实体?
    • 是的,你也应该在模型中拥有它。
    • 是的 :) 问题是“如何”? :) 具有 3 个标量字段的新实体?或者?如何为这些事物分配关联?
    • 答案已更新。让您的生活更轻松并获得POCO generation extension(s)
    猜你喜欢
    • 1970-01-01
    • 2011-06-18
    • 2011-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-01
    • 1970-01-01
    相关资源
    最近更新 更多