none
About a possible pattern in EF RRS feed

  • Question

  • Hi. I know that writing this small piece of code:

    Public Class MyContext Inherits DbContext

        Public Property Orders As DbSet(Of Order)

        Public Property Details As DbSet(Of Detail)

        Public Sub New(ByVal DatabaseName As String)
            MyBase.New(DatabaseName)
        End Sub

    End Class

    end using this instruction:

    Dim MC As MyContext = New MyContext("Name")

    I get the fully implemented database. I was wondering which is the pattern EF uses in order to create the two DbSet I need. EF is able to create a generic subclass of DbContext without the programmer has to implement a factory method. Stranger then that, EF is able to instantiate a generic number of DbSet as well. I have studied the creational patterns but I can not find anything that could explain this technique. In other words, anyone knows a pattern which lets me create a generic subclass containing a generic number of properties? Thanks.


    • Edited by speculor Wednesday, May 1, 2013 9:27 AM
    Wednesday, May 1, 2013 9:14 AM

Answers

  • It could be something like this:

    Public Class Person

        Private _Name As String

        Public Property Name() As String
            Get
                Return _Name
            End Get
            Set(ByVal value As String)
                _Name = value
            End Set
        End Property

        Sub New()
            Dim T As Type = Me.GetType
            Dim PI As PropertyInfo() = T.GetProperties()
            For Each P As PropertyInfo In PI
                P.SetValue(Me, "...", Nothing)
            Next
        End Sub

    End Class

    Public Class Men Inherits Person

        Private _Surname As String

        Public Property Surname() As String
            Get
                Return _Surname
            End Get
            Set(ByVal value As String)
                _Surname = value
            End Set
        End Property

        Sub New()
            MyBase.New()
        End Sub

    End Class

    Module Module1

        Sub Main()
            Dim M As Men = New Men
            System.Console.WriteLine(M.Name)
            System.Console.WriteLine(M.Surname)
        End Sub

    End Module

    Even though my example is a little odd, it works. Is it possible that the code inside the class DbContext is quite the same?

    Wednesday, May 1, 2013 4:50 PM

All replies

  • Actually, since the DbContext is not abstract, a factory method is not necessary. But how does EF know how many DbSet are present in a derived class? May be using Reflection, but if this is the case, there must be a method which returns the types which derive from DbContext. Sorry if I am making some conceptual mistake.


    • Edited by speculor Wednesday, May 1, 2013 10:00 AM
    Wednesday, May 1, 2013 9:58 AM
  • Sorry, I have realized that a factory method is not necessary at all, even if the DbContext was abstract. The problem is that I am still a little confused about the creational patterns. Anyway, my problem is simpler than I imagined: I do not simply understand how DbContext knows the number and the generic types of the different DbSet a generic subclass contains. I ask because I instantiate only the class derived from DbContext, not the different DbSet I have defined in it. Thanks.
    • Edited by speculor Wednesday, May 1, 2013 10:35 AM
    Wednesday, May 1, 2013 10:31 AM
  • It could be something like this:

    Public Class Person

        Private _Name As String

        Public Property Name() As String
            Get
                Return _Name
            End Get
            Set(ByVal value As String)
                _Name = value
            End Set
        End Property

        Sub New()
            Dim T As Type = Me.GetType
            Dim PI As PropertyInfo() = T.GetProperties()
            For Each P As PropertyInfo In PI
                P.SetValue(Me, "...", Nothing)
            Next
        End Sub

    End Class

    Public Class Men Inherits Person

        Private _Surname As String

        Public Property Surname() As String
            Get
                Return _Surname
            End Get
            Set(ByVal value As String)
                _Surname = value
            End Set
        End Property

        Sub New()
            MyBase.New()
        End Sub

    End Class

    Module Module1

        Sub Main()
            Dim M As Men = New Men
            System.Console.WriteLine(M.Name)
            System.Console.WriteLine(M.Surname)
        End Sub

    End Module

    Even though my example is a little odd, it works. Is it possible that the code inside the class DbContext is quite the same?

    Wednesday, May 1, 2013 4:50 PM
  • Hi specular,

    Yes, there is an internal method to get the all the DbSet using reflection. However I'm afraid we cannot tell you the detailed information.

    Thanks for sharing your ideas here. Have a nice day!

    Best regards,


    Chester Hong
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Sunday, May 5, 2013 9:04 PM
    Moderator