多对多、poco、ef4

发布于 2024-10-08 18:54:46 字数 441 浏览 0 评论 0 原文

我有 3 个实体:

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

但现在我需要将这些对象相互关联。 换句话说,每个商品可以有许多人担任许多角色。 我在数据库中有一个包含 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 格式映射它?

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

终遇你 2024-10-15 18:54:46

我相信您必须创建另一个 PersonRoles 标头,在其中存储 Person-Role 关系,然后通过以下标头访问 person+role:

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,所以我对上面的伪代码表示歉意,但我希望你能明白。

更新

它应该是这样的:(

<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 实体生成器

我刚刚复制了“良好”ID 和导航。财产不过应该是他们三个人的。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文