首页 > 代码库 > 以继承方式让EF使用泛型
以继承方式让EF使用泛型
Entity Framework 无法直接使用泛型,但是我们可以通过使用继承的办法来间接使用泛型。
Imports System.Data.Entity
Imports System.ComponentModel.DataAnnotations
Module Module1
Sub Main()
Database.SetInitializer(New MigrateDatabaseToLatestVersion(Of mycontext, Migrations.Configuration)())
Dim myc As New mycontext
Dim myg As New GenericDerivative With {.x = "uuu", .y = "cdd"}
Dim myge As New GenericProperty With {.GenericString = myg}
myc.Gp.Add(myge)
myc.SaveChanges()
Console.WriteLine("create Successfully")
For Each w In myc.Gp
Console.WriteLine(w.GenericString.x)
Next
Console.ReadKey()
End Sub
End Module
Public Class mycontext
Inherits DbContext
Public Property Gp As DbSet(Of GenericProperty)
End Class
Public Class GenericProperty
<Key()>
Public Property id As Integer
Public Overridable Property GenericString As GenericDerivative
End Class
Public Class Generic(Of T)
<Key()>
Public Property id As Integer
Public Property x As T
Public Property y As T
End Class
Public Class check
<Key()>
Public Property id As Integer
Public Property u As String
Public Property t As Boolean
End Class
Public Class GenericDerivative
Inherits Generic(Of String)
End Class