2015-11-02 106 views
2

我新來到這裏(我知道這個網站很長,但這是我第一次實際上要求一些)。EF6 - 添加對象到表動態

組件,我使用: - EF6,的DevExpress XtraGrid中

好......所以,我要的是那種做到這一點, 我有1種形式有多個表,我將有能夠從每個NavigationBar中添加和刪除。

我知道該怎麼做,我只是需要一種方法來跳過選擇的情況。

下面是一些代碼,

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 

因此,而不是寫每增加對每一種情況下,我不得不做一些事情像那樣。 是否可能或者我會堅持選擇案例?

在此先感謝,並對不起,如果我的英語不好(我不是本地人)。

編輯1: 馬克在評論中提到,我們可以在C# 但在VB中這是行不通的使用this ...

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 
+0

歡迎來到SO!你的英語很好,你的問題很好解釋,所以工作做得很好。這個問題已經出現了幾次,http://stackoverflow.com/q/283​​64376/1158842,http://stackoverflow.com/q/5295554/1158842,http://stackoverflow.com/q/8138070/ 1158842,http://stackoverflow.com/q/17879206/1158842。這些解決你的問題嗎? –

+0

嗯,我想我必須堅持從我從這些帖子看到的選擇案例,EF不喜歡dyncamic的東西... 感謝您的幫助,如果有人有任何想法如何使這更好方式,我都是耳朵。 –

+0

您可以在DataSet級別添加實體,但是您必須自己做很多處理。 – DevilSuichiro

回答

1

與馬克的幫助下,我終於得到這個加工。

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 

感謝大家的幫忙!