Entity Framework : Why this extension is not usable ?
-
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.ObjectsModule 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 -
This exactly the same code that I have used. but I get the error that I have posted before.
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 -
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 KreskowiakThe 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()
...
Nextso in this situation , do you have an idea why i have those strange looking proxy objects like : MyObject_F2FFE64DA472EB2B2BDF7E143DE887D3845AD9D1731FD3107937062AC0C2E4BB Thank you !
-
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()
...
Nextso in this situation , do you have an idea why i have those strange looking proxy objects like : MyObject_F2FFE64DA472EB2B2BDF7E143DE887D3845AD9D1731FD3107937062AC0C2E4BB Thank you !
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 -
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 -
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 -
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 -
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 ?
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 -
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 ?
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).
-
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).