【发布时间】:2016-02-02 15:11:58
【问题描述】:
我是新来的(我知道这个网站很久了,但这是我第一次真正地问一些问题)。
我正在使用的组件:- EF6、Devexpress XtraGrid
好的...所以,我想要的是这样做, 我有一个包含多个表格的表格,我必须能够在每个表格的 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# 中使用this 但在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
【问题讨论】:
-
欢迎来到 SO!你的英语很好,你的问题得到了很好的解释,所以做得很好。这个问题之前出现过几次,stackoverflow.com/q/28364376/1158842、stackoverflow.com/q/5295554/1158842、stackoverflow.com/q/8138070/1158842、stackoverflow.com/q/17879206/1158842。这些是否解决了您的问题?
-
好吧,我想我必须坚持从我从那些帖子中读到的选择案例,EF 不喜欢动态的东西......无论如何,感谢您的帮助,如果有人知道如何为了以更好的方式做到这一点,我全神贯注。
-
您可以在 DataSet 级别添加实体,但是您必须自己做很多处理。
-
@DevilSuichiro 我不能在这个项目上使用 DataSet,很容易将网格绑定到那个(DevExpress 用 DataSets 创造奇迹)但我不能在这里使用它抱歉,谢谢建议。
-
变量
db的类型是什么?
标签: vb.net entity-framework devexpress-windows-ui