码迷,mamicode.com
首页 > 其他好文 > 详细

以继承方式让EF使用泛型

时间:2014-05-26 16:38:59      阅读:237      评论:0      收藏:0      [点我收藏+]

标签:c   class   ext   a   int   com   

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

以继承方式让EF使用泛型,布布扣,bubuko.com

以继承方式让EF使用泛型

标签:c   class   ext   a   int   com   

原文地址:http://www.cnblogs.com/jiangzs/p/3751734.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!