Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Entity Framework : Why this extension is not usable ?

Entity Framework : Why this extension is not usable ?

Scheduled Pinned Locked Moved Visual Basic
helpcsharpasp-netjson
18 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D dilkonika

    Hello ! I'm using entity framework 6 with Vb.net 2013. I'm trying to add an extension. This is the code :

    Imports System.ComponentModel
    Imports System.Collections
    Imports System.Data.Entity.Core.Objects.DataClasses
    Imports System.Runtime.Serialization
    Imports System.IO
    Imports System.Reflection

    Module Extensions
    _
    Public Function Clone(Of T As EntityObject)(source As T) As T
    Dim ser = New DataContractSerializer(GetType(T))
    Using stream = New MemoryStream()
    ser.WriteObject(stream, source)
    stream.Seek(0, SeekOrigin.Begin)
    Return CType(ser.ReadObject(stream), T)
    End Using
    End Function
    End Module

    But if I try to use like this :

    Dim litm, newitm as MyObject
    For Each litm In itemlist
    newitm = litm.Clone()
    ...
    Next

    I'm getting this error :

    'Clone' is not a member of 'TheProg.MyObject'

    What's the problem ?

    D Offline
    D Offline
    Dave Kreskowiak
    wrote on last edited by
    #2

    Your ``MyObject`` class doesn't have a ``Clone()`` method. It's not supplied to your class automatically, after all how would a default implementation know how to correctly copy your class? It wouldn't. YOU have to implement a ``Clone()`` method yourself.

    A guide to posting questions on CodeProject

    Click this: Asking questions is a skill. Seriously, do it.
    Dave Kreskowiak

    D 1 Reply Last reply
    0
    • D Dave Kreskowiak

      Your ``MyObject`` class doesn't have a ``Clone()`` method. It's not supplied to your class automatically, after all how would a default implementation know how to correctly copy your class? It wouldn't. YOU have to implement a ``Clone()`` method yourself.

      A guide to posting questions on CodeProject

      Click this: Asking questions is a skill. Seriously, do it.
      Dave Kreskowiak

      D Offline
      D Offline
      dilkonika
      wrote on last edited by
      #3

      sorry , but why the method Clone is not added as extension ? Have I done something wrong constructing the extension ?

      D 1 Reply Last reply
      0
      • D dilkonika

        sorry , but why the method Clone is not added as extension ? Have I done something wrong constructing the extension ?

        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #4

        Wow an I tired. It's 2:00am here. I didn't even notice the Clone method name... What does your ``MyObject`` class look like? Are you using Database First, Code First, or what?

        A guide to posting questions on CodeProject

        Click this: Asking questions is a skill. Seriously, do it.
        Dave Kreskowiak

        D 1 Reply Last reply
        0
        • D Dave Kreskowiak

          Wow an I tired. It's 2:00am here. I didn't even notice the Clone method name... What does your ``MyObject`` class look like? Are you using Database First, Code First, or what?

          A guide to posting questions on CodeProject

          Click this: Asking questions is a skill. Seriously, do it.
          Dave Kreskowiak

          D Offline
          D Offline
          dilkonika
          wrote on last edited by
          #5

          I'm using Database First.

          Partial Public Class Myobject
          Public Property id As Integer
          Public property name as string
          Public Overridable Property chld As ICollection(Of chld) = New HashSet(Of chld)
          End Class

          Partial Public Class chld
          Public Property id As Integer
          Public Property date1 as DateTime
          Public Property quantity as Integer
          Public Property ParentID as integer
          Public Overridable Property MyObj1 As MyObject
          End Class

          D 1 Reply Last reply
          0
          • D dilkonika

            I'm using Database First.

            Partial Public Class Myobject
            Public Property id As Integer
            Public property name as string
            Public Overridable Property chld As ICollection(Of chld) = New HashSet(Of chld)
            End Class

            Partial Public Class chld
            Public Property id As Integer
            Public Property date1 as DateTime
            Public Property quantity as Integer
            Public Property ParentID as integer
            Public Overridable Property MyObj1 As MyObject
            End Class

            D Offline
            D Offline
            Dave Kreskowiak
            wrote on last edited by
            #6

            It doesn't work because Database First entity objects inherit from ``Object``, not ``EntityObject``. Your current Close method would work just fine for a Code First EF model. The only way to get the ``Clone()`` to work is to remove the "As EntityObject" restriction in the function header. Of course, this is going to make your ``Clone()`` method available for every type in the application.

            A guide to posting questions on CodeProject

            Click this: Asking questions is a skill. Seriously, do it.
            Dave Kreskowiak

            D 1 Reply Last reply
            0
            • D Dave Kreskowiak

              It doesn't work because Database First entity objects inherit from ``Object``, not ``EntityObject``. Your current Close method would work just fine for a Code First EF model. The only way to get the ``Clone()`` to work is to remove the "As EntityObject" restriction in the function header. Of course, this is going to make your ``Clone()`` method available for every type in the application.

              A guide to posting questions on CodeProject

              Click this: Asking questions is a skill. Seriously, do it.
              Dave Kreskowiak

              D Offline
              D Offline
              dilkonika
              wrote on last edited by
              #7

              I remove the "As entityobject". Now , on runtime , I get this error on the line : ser.WriteObject(stream, source) :

              An unhandled exception of type 'System.Runtime.Serialization.SerializationException' occurred in System.Runtime.Serialization.dll

              Additional information: Type 'System.Data.Entity.DynamicProxies.MyObject_F2FFE64DA472EB2B2BDF7E143DE887D3845AD9D1731FD3107937062AC0C2E4BB' with data contract name 'MyObject_F2FFE64DA472EB2B2BDF7E143DE887D3845AD9D1731FD3107937062AC0C2E4BB:http://schemas.datacontract.org/2004/07/System.Data.Entity.DynamicProxies' is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.

              D 1 Reply Last reply
              0
              • D dilkonika

                I remove the "As entityobject". Now , on runtime , I get this error on the line : ser.WriteObject(stream, source) :

                An unhandled exception of type 'System.Runtime.Serialization.SerializationException' occurred in System.Runtime.Serialization.dll

                Additional information: Type 'System.Data.Entity.DynamicProxies.MyObject_F2FFE64DA472EB2B2BDF7E143DE887D3845AD9D1731FD3107937062AC0C2E4BB' with data contract name 'MyObject_F2FFE64DA472EB2B2BDF7E143DE887D3845AD9D1731FD3107937062AC0C2E4BB:http://schemas.datacontract.org/2004/07/System.Data.Entity.DynamicProxies' is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.

                D Offline
                D Offline
                Dave Kreskowiak
                wrote on last edited by
                #8

                I don't know what you've got wrong because I can't replicated it. This is the modified extension code I used:

                Imports System.Data.Entity.Core.Objects.DataClasses
                Imports System.Runtime.Serialization
                Imports System.IO
                Imports System.Runtime.CompilerServices
                Imports System.Data.Objects

                Module Extensions

                Public Function Clone(Of T)(source As T) As T
                    Dim ser = New DataContractSerializer(GetType(T))
                    Using stream = New MemoryStream()
                        ser.WriteObject(stream, source)
                        stream.Seek(0, SeekOrigin.Begin)
                        Return CType(ser.ReadObject(stream), T)
                    End Using
                End Function
                

                End Module

                A guide to posting questions on CodeProject

                Click this: Asking questions is a skill. Seriously, do it.
                Dave Kreskowiak

                D 1 Reply Last reply
                0
                • D Dave Kreskowiak

                  I don't know what you've got wrong because I can't replicated it. This is the modified extension code I used:

                  Imports System.Data.Entity.Core.Objects.DataClasses
                  Imports System.Runtime.Serialization
                  Imports System.IO
                  Imports System.Runtime.CompilerServices
                  Imports System.Data.Objects

                  Module Extensions

                  Public Function Clone(Of T)(source As T) As T
                      Dim ser = New DataContractSerializer(GetType(T))
                      Using stream = New MemoryStream()
                          ser.WriteObject(stream, source)
                          stream.Seek(0, SeekOrigin.Begin)
                          Return CType(ser.ReadObject(stream), T)
                      End Using
                  End Function
                  

                  End Module

                  A guide to posting questions on CodeProject

                  Click this: Asking questions is a skill. Seriously, do it.
                  Dave Kreskowiak

                  D Offline
                  D Offline
                  dilkonika
                  wrote on last edited by
                  #9

                  This exactly the same code that I have used. but I get the error that I have posted before.

                  D 1 Reply Last reply
                  0
                  • D dilkonika

                    This exactly the same code that I have used. but I get the error that I have posted before.

                    D Offline
                    D Offline
                    Dave Kreskowiak
                    wrote on last edited by
                    #10

                    It just dawned on me why you're getting that error. The object you're trying to Clone is referencing other object in the database that haven't been re-hydrated by EF yet. That's where those strange looking proxy objects are coming from. In order to get it to work, you have to load the objects being referenced before you try and Close the base object.

                    A guide to posting questions on CodeProject

                    Click this: Asking questions is a skill. Seriously, do it.
                    Dave Kreskowiak

                    D 1 Reply Last reply
                    0
                    • D Dave Kreskowiak

                      It just dawned on me why you're getting that error. The object you're trying to Clone is referencing other object in the database that haven't been re-hydrated by EF yet. That's where those strange looking proxy objects are coming from. In order to get it to work, you have to load the objects being referenced before you try and Close the base object.

                      A guide to posting questions on CodeProject

                      Click this: Asking questions is a skill. Seriously, do it.
                      Dave Kreskowiak

                      D Offline
                      D Offline
                      dilkonika
                      wrote on last edited by
                      #11

                      The way I'm getting the object that i want to clone is this : -I have a listbox bound to a bindingsource (that have as datasource = (From t in context.Myobjects where t.id>5 select t ).Tolist - The user select several items from listbox. - I have a listitm=New list(of Myobject) , where i add one by one the items selected from listbox. - After i have the code that i posted to my question :

                      Dim litm, newitm as MyObject
                      For Each litm In itemlist
                      newitm = litm.Clone()
                      ...
                      Next

                      so in this situation , do you have an idea why i have those strange looking proxy objects like : MyObject_F2FFE64DA472EB2B2BDF7E143DE887D3845AD9D1731FD3107937062AC0C2E4BB Thank you !

                      D 1 Reply Last reply
                      0
                      • D dilkonika

                        The way I'm getting the object that i want to clone is this : -I have a listbox bound to a bindingsource (that have as datasource = (From t in context.Myobjects where t.id>5 select t ).Tolist - The user select several items from listbox. - I have a listitm=New list(of Myobject) , where i add one by one the items selected from listbox. - After i have the code that i posted to my question :

                        Dim litm, newitm as MyObject
                        For Each litm In itemlist
                        newitm = litm.Clone()
                        ...
                        Next

                        so in this situation , do you have an idea why i have those strange looking proxy objects like : MyObject_F2FFE64DA472EB2B2BDF7E143DE887D3845AD9D1731FD3107937062AC0C2E4BB Thank you !

                        D Offline
                        D Offline
                        Dave Kreskowiak
                        wrote on last edited by
                        #12

                        I just told you why. The object has a reference to another related object that EF hasn't loaded.

                        A guide to posting questions on CodeProject

                        Click this: Asking questions is a skill. Seriously, do it.
                        Dave Kreskowiak

                        D 1 Reply Last reply
                        0
                        • D Dave Kreskowiak

                          I just told you why. The object has a reference to another related object that EF hasn't loaded.

                          A guide to posting questions on CodeProject

                          Click this: Asking questions is a skill. Seriously, do it.
                          Dave Kreskowiak

                          D Offline
                          D Offline
                          dilkonika
                          wrote on last edited by
                          #13

                          Actually , i have loaded all referenced object , but i think some of them are loaded only with some of the properties. MyObjects - ob1 I have loaded ob1 like below : Myob1list=(From t in context.ob1s Select t.id,t.name).Tolist May be this the problem , and if yes , what should i do ? Thank you !

                          D 1 Reply Last reply
                          0
                          • D dilkonika

                            Actually , i have loaded all referenced object , but i think some of them are loaded only with some of the properties. MyObjects - ob1 I have loaded ob1 like below : Myob1list=(From t in context.ob1s Select t.id,t.name).Tolist May be this the problem , and if yes , what should i do ? Thank you !

                            D Offline
                            D Offline
                            Dave Kreskowiak
                            wrote on last edited by
                            #14

                            You have to load the child objects in your query:

                            Myob1list = (From t in context.ob1s.Include("chld")
                                         Select t).ToList()
                            

                            (I'm converting C# code from memory. I haven't touched VB.NET in a few years now.)

                            A guide to posting questions on CodeProject

                            Click this: Asking questions is a skill. Seriously, do it.
                            Dave Kreskowiak

                            D 1 Reply Last reply
                            0
                            • D Dave Kreskowiak

                              You have to load the child objects in your query:

                              Myob1list = (From t in context.ob1s.Include("chld")
                                           Select t).ToList()
                              

                              (I'm converting C# code from memory. I haven't touched VB.NET in a few years now.)

                              A guide to posting questions on CodeProject

                              Click this: Asking questions is a skill. Seriously, do it.
                              Dave Kreskowiak

                              D Offline
                              D Offline
                              dilkonika
                              wrote on last edited by
                              #15

                              Sorry , but I'm using lazyloading. and I've read that these proxy objects , are related with using of lazyloading. So , why all other actions that I do with my objects works without problems, and this Clone action produce errors ?

                              D S 2 Replies Last reply
                              0
                              • D dilkonika

                                Sorry , but I'm using lazyloading. and I've read that these proxy objects , are related with using of lazyloading. So , why all other actions that I do with my objects works without problems, and this Clone action produce errors ?

                                D Offline
                                D Offline
                                Dave Kreskowiak
                                wrote on last edited by
                                #16

                                dilkonika wrote:

                                Sorry , but I'm using lazyloading. and I've read that these proxy objects , are related with using of lazyloading.

                                Yeah, I can see that.

                                dilkonika wrote:

                                So , why all other actions that I do with my objects works without problems, and this Clone action produce errors ?

                                Because the proxy classes are dynamically generated by EF and are not serializable.

                                A guide to posting questions on CodeProject

                                Click this: Asking questions is a skill. Seriously, do it.
                                Dave Kreskowiak

                                1 Reply Last reply
                                0
                                • D dilkonika

                                  Sorry , but I'm using lazyloading. and I've read that these proxy objects , are related with using of lazyloading. So , why all other actions that I do with my objects works without problems, and this Clone action produce errors ?

                                  S Offline
                                  S Offline
                                  Sascha Lefevre
                                  wrote on last edited by
                                  #17

                                  dilkonika wrote:

                                  Sorry , but I'm using lazyloading.

                                  That doesn't mean you can't tell EF it should eager-load dependencies when you need it (which is what Include(..) does).

                                  D 1 Reply Last reply
                                  0
                                  • S Sascha Lefevre

                                    dilkonika wrote:

                                    Sorry , but I'm using lazyloading.

                                    That doesn't mean you can't tell EF it should eager-load dependencies when you need it (which is what Include(..) does).

                                    D Offline
                                    D Offline
                                    dilkonika
                                    wrote on last edited by
                                    #18

                                    yes , but when I call the extension , these objects are already loaded in the way that I have described.

                                    1 Reply Last reply
                                    0
                                    Reply
                                    • Reply as topic
                                    Log in to reply
                                    • Oldest to Newest
                                    • Newest to Oldest
                                    • Most Votes


                                    • Login

                                    • Don't have an account? Register

                                    • Login or register to search.
                                    • First post
                                      Last post
                                    0
                                    • Categories
                                    • Recent
                                    • Tags
                                    • Popular
                                    • World
                                    • Users
                                    • Groups