New Posts New Posts RSS Feed: How do I clone an POCO Entity using CF?
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

How do I clone an POCO Entity using CF?

 Post Reply Post Reply
Author
jradxl View Drop Down
Newbie
Newbie


Joined: 14-Jun-2011
Posts: 30
Post Options Post Options   Quote jradxl Quote  Post ReplyReply Direct Link To This Post Topic: How do I clone an POCO Entity using CF?
    Posted: 04-Sep-2012 at 3:54pm
How do I clone an POCO Entity when using DF Code First

Ok, in my abstract EntityBase I can try to implement ICloneable
but the methods I have found use serialisation, which when I mark the POCO as [Serializable], DF stops working!

Suggestions?
Thanks
John

Back to Top
jradxl View Drop Down
Newbie
Newbie


Joined: 14-Jun-2011
Posts: 30
Post Options Post Options   Quote jradxl Quote  Post ReplyReply Direct Link To This Post Posted: 05-Sep-2012 at 3:52am
OK, I've found In need both [Serializable] and [DataContract] explictly.
Was assuming that PostSharp did the hard work for me!
And my Entity does have a public parameterless constructor.

POCO types must be serializable:  
    1) Mark the class and members with the DataContract and DataMember attributes, or
    2) The public properties of the class will be serializable if the class has a public parameterless constructor. 

But now I've got a problem with XML Serialisation
with this error
IdeaBlade.EntityModel.EntityAspect cannot be serialized because it does not have a parameterless constructor.
 
XmlSerializer formatter = new XmlSerializer((dfclone.EntityAspect.Entity).GetType());

Any suggestions?

thanks





Back to Top
jradxl View Drop Down
Newbie
Newbie


Joined: 14-Jun-2011
Posts: 30
Post Options Post Options   Quote jradxl Quote  Post ReplyReply Direct Link To This Post Posted: 05-Sep-2012 at 6:22am
I seem to have it with

            DataContractSerializer serializer = new DataContractSerializer(dfclone.GetType());
            XDocument document = new XDocument();
            using (XmlWriter xmlWriter = document.CreateWriter())
            {
                serializer.WriteObject(xmlWriter, dfclone);
            }
            var xmldoc = document.Root.ToString();

But XML has the EntityAspect present.
Will it always be between <AAAA_EntityAspectForSerialization ></> so I can remover from XML?

thanks

 <Address xmlns:i="http://www.w3.org/2001/XMLSchema-instance" z:Id="i1" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/">
  <AAAA_EntityAspectForSerialization xmlns:d1p1="http://ideablade.com/EntityModel" z:Id="i2" xmlns="http://schemas.datacontract.org/2004/07/SimpleModel">
    <d1p1:LoadedNavigationPropertyNames xmlns:d2p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays" i:nil="true" />
    <d1p1:ManyToManyChangeMap i:nil="true" />
    <d1p1:OriginalValuesMap i:nil="true" />
    <d1p1:ValidationErrorsToSerialize xmlns:d2p1="http://ideablade.com/Validation" />
    <d1p1:EntityTypeWrapper xmlns:d2p1="http://ideablade.com/Core">
      <d2p1:AssemblyQualifiedName>SimpleModel.Name, SimpleModel, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null</d2p1:AssemblyQualifiedName>
      <d2p1:IsNull>false</d2p1:IsNull>
    </d1p1:EntityTypeWrapper>
    <d1p1:EntityStateValue>Detached</d1p1:EntityStateValue>
  </AAAA_EntityAspectForSerialization>
etc, etc.

Back to Top
sbelini View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 13-Aug-2010
Location: Oakland
Posts: 786
Post Options Post Options   Quote sbelini Quote  Post ReplyReply Direct Link To This Post Posted: 05-Sep-2012 at 11:56am
Hi jradxl,
 
EntityAspect will always be present in this case.
I would suggest to use a surrogate, so you can only serialize the properties you wish.
 
Regards,
   Silvio.
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down