Search code examples
vb.netentity-frameworkdevexpress-windows-ui

EF6 - add objects to tables dynamicaly


I'm new around here (I know this site for long but it's my first time actually asking somehting).

Components that I'm using: - EF6, Devexpress XtraGrid

Ok... so, what I want is to kind of do this, I have 1 form with multiple tables, and which I will have to be able to add and delete from each's NavigationBar.

I know how to do it, I just need a way to skip the select case.

Here's some code,

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

What I want to do with that is kind of this:

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

So instead of writing every add for each case, I just had to do something like that. Is it possible or am I going to stick with the select case?

Thanks in advance, and sorry if my english is bad (I'm not native).

EDIT 1: as Mark mentioned in a comment we could use this in C# but in VB it doesn't work...

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

Any ideas how to do this in VB?

EDIT 2: Ok, so I got it working like this

Public Class GenericRepository(Of T As Class)

now my problem is how to get the type from the object

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

Solution

  • With Mark's help I finally got this working.

    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
    

    Thanks for everyone's help!