从ICollections(vb.NET)映射表(实体框架)之间的关系

时间:2016-06-13 10:33:01

标签: vb.net entity-framework associations

我正在这里开展一个大项目,我会尽可能地简化这个问题。

它基本上是一个DLL,它将每个用户的权限保存到数据库并将其返回给应用程序,这整个事情使用实体框架。

这里是Classes,我不会插入所有这些,但只有两个会创建一个n:m Relashionship:

    Public Class Group
Inherits Object
<Key>
Public Property GroupID As Long
<Required>
Public Property GroupName As String
Public Overridable Property Rights As ICollection(Of Rights) = New HashSet(Of Rights)

另一个是:

Public Class Rights
<Key>
Public Property ID As Long
<Required>
Public Property Number As Integer
<Required>
 Public Property Name As String
Public Property Description As String
Public Overridable Property Groups As ICollection(Of Groups) = New HashSet(Of Groups)

到目前为止一切顺利。这整个数据来自* .csv文件,我将其作为DataTable读入,将其返回到另一个获取DataTable的DLL,从每个Class创建对象并将对象作为ICollection返回,这也可以正常工作。

这里有两个函数来自这个类,所以从DataTable中获取数据的两个函数会从中生成对象并将其作为ICollection返回:

Public Function ReturnAllGroups(ByVal dt As DataTable) As ICollection(Of Group) Implements IInfoServiceUtil.ReturnAllGroups

    If dt Is Nothing Then
        Throw New ArgumentNullException("DataTable can't be nothing", "dt")
    End If

    If Not dt.Columns.Contains("Profile") Then
        Throw New ArgumentException("Couldn't find Column Profile")
    End If

    Dim profilNames = From row In dt.AsEnumerable()
    Select row.Field(Of String)("Profile") Distinct

    Dim coll As ICollection(Of Group) = New List(Of Group)

    For Each name In profilNames
        If name <> Nothing Then

            Dim group As Group= New Group()

            groupg.GroupName = name
            coll.Add(group)
        End If
    Next
    Return coll

End Function

在这里另一个onde:

 Public Function ReturnAllRights(ByVal dt As DataTable) As ICollection(Of Rights) Implements IInfoServiceUtil.ReturnAllRights
    If dt Is Nothing Then
        Throw New ArgumentNullException("DataTable can't be nothing!", "dt")
    End If

    If Not dt.Columns.Contains("RightName") Then
        Throw New ArgumentException("Couldn't find column RightName!")
    End If

    If Not dt.Columns.Contains("RightNumber") Then
        Throw New ArgumentException("Couldn't find column RightNumber!")
    End If

    Dim query = From dr As DataRow In dt.DefaultView.ToTable(True, "RightName", "RightNumber")

    Dim coll As ICollection(Of Rights) = New List(Of Rights)
    For Each row In query
        Dim right As Rights = New Rights()

        If IsNumeric(row.Item("RightNumber")) Then

            With right
                .Name = row.Item("RightName")
                .Number= row.Item("RightNumber")
                coll.Add(right)
            End With

        End If
    Next

    Return coll
End Function

这也很好。在这一部分,我被困了,因为我不知道如何从集团和权利表中建立关联表。

这将是一个像这样的函数:

Public Function CreateAssociation(group As ICollection(Of Groups), right As ICollection(Of Rights), dt As DataTable) As ICollection(Of Group)
    'TODO
    Return coll
End Function

很抱歉,如果这是一个很长的问题,但任何帮助都会非常感激。我也有更多的桌子,但是一旦我知道如何做一个,其他人将工作得很好。 非常感谢!

0 个答案:

没有答案