EF6 - 动态地将对象添加到表中

EF6 - add objects to tables dynamicaly

我是新来的(我知道这个网站很久了,但这是我第一次真正问一些问题)。

我正在使用的组件:- EF6、Devexpress XtraGrid

好的...所以,我想要做的就是这样做, 我有 1 个表格和多个表格,我必须能够从每个表格的 NavigationBar 添加和删除表格。

我知道怎么做,我只需要一种方法来跳过 select 案例。

这是一些代码,

Private Sub ListChanged(sender As Object, e As System.ComponentModel.ListChangedEventArgs)
    If e.ListChangedType = System.ComponentModel.ListChangedType.ItemAdded Then
        Dim bList As BindingList(Of Object) = TryCast(sender, BindingList(Of Object))
        Dim m As Object = bList.LastOrDefault()
        If m IsNot Nothing Then
            Select Case _curentPageIndex
                Case 0 : db.GESTARM.Add(m)
                Case 1 : 'Other table add
                Case 2 : 'Other table add
            End Select
        End If
    End If
End Sub

我想用它做的是这样的:

Private Sub ListChanged(sender As Object, e As System.ComponentModel.ListChangedEventArgs)
    If e.ListChangedType = System.ComponentModel.ListChangedType.ItemAdded Then
        Dim bList As BindingList(Of Object) = TryCast(sender, BindingList(Of Object))
        Dim m As Object = bList.LastOrDefault()
        'somehow get the table (type) of the entry through the m object
        If m IsNot Nothing Then
            db.<Table>.Add(m)
        End If
    End If
End Sub

所以我不必为每个案例编写每个添加,我只需要做类似的事情。 有可能还是我会坚持 select 案例?

提前致谢,如果我的英语不好(我不是母语),请见谅。

编辑 1: 正如 Mark 在评论中提到的,我们可以在 C# 中使用 但在 VB 它不起作用...

Public Class GenericRepository(Of T)
Implements IDisposable
Friend context As GestProGest1Entities
Friend dbSet As Entity.DbSet(Of T) ' Gives error on T "Type argument 'T' does not satisfy the 'Class' constraint for type parameter 'TEntity'"

Public Sub Dispose() Implements IDisposable.Dispose
    If context IsNot Nothing Then
        context.Dispose()
        context = Nothing
    End If
End Sub

Public Sub New(context As GestProGest1Entities)
    Me.context = context
    Me.dbSet = context.Set(Of T)() ' Gives error on T "Type argument 'T' does not satisfy the 'Class' constraint for type parameter 'TEntity'"
End Sub

Public Overridable Sub Insert(entity As T)
    dbSet.Add(entity)
    context.SaveChanges()
End Sub
End Class

知道如何在 VB 中做到这一点吗?

编辑 2: 好的,所以我让它像这样工作

Public Class GenericRepository(Of T As Class)

现在我的问题是如何从对象中获取类型

Private Sub ListChanged(sender As Object, e As System.ComponentModel.ListChangedEventArgs)
    If e.ListChangedType = System.ComponentModel.ListChangedType.ItemAdded Then
        Dim bList As BindingList(Of Object) = TryCast(sender, BindingList(Of Object))
        Dim m As Object = bList.LastOrDefault()
        Dim myType As Type = m.GetType()
        Dim table As New GenericRepository(Of myType)(db) 'Doesn't accept myType here...
        table.Insert(m)
    End If
End Sub

在 Mark 的帮助下,我终于成功了。

Private Sub ListChanged(sender As Object, e As System.ComponentModel.ListChangedEventArgs)
  If e.ListChangedType = System.ComponentModel.ListChangedType.ItemAdded Then
      Dim m As Object = sender(sender.count - 1)
      db.Set(m.GetType()).Add(m)
  End If 
End Sub

感谢大家的帮助!