none
deserialize is creating a exception on object creation RRS feed

  • Question

  • Hi,

    I'm trying to detach a object from a datatcontext, just to update an object.

    But when trying this every kind of function with serialize and deserialize gives me a exception after the constructor.

    The functions i've tried:

     

    MemoryStream test = new MemoryStream();
    test = Serialize(entiteit);
    T nieuw = Deserialize<T>(test as MemoryStream);
    tabel.Attach(entiteit, true ); 
    
    public static MemoryStream Serialize<T>(T obj) 
    { 
      DataContractSerializer s = new DataContractSerializer(typeof (T));  
      MemoryStream ms = new MemoryStream(); 
      s.WriteObject(ms, obj); 
      return ms; 
    }
    
    public static T Deserialize<T>(MemoryStream ms) 
    { 
      DataContractSerializer s = new DataContractSerializer(typeof (T));   
      ms.Position = 0; 
      T retObject = (T)s.ReadObject(ms); 
      return retObject; 
    } 
    

    AND

     

                        
    detach(entiteit); 
    tabel.Attach(entiteit, true );
    
    private T detach(T entiteit) 
    { 
      DataContractSerializer dcs = new DataContractSerializer(entiteit.GetType());
      if (entiteit == null ) return null ; 
    
    StringBuilder sb = new StringBuilder(); 
    
    using (XmlWriter xmlw = XmlWriter.Create(sb)) 
    { 
    dcs.WriteObject(xmlw, entiteit); xmlw.Close(); 
    } 
    string serialized = sb.ToString(); 
    Console.WriteLine(serialized); 
    
    using (StringReader sr = new StringReader(serialized)) 
    using (XmlTextReader xmltr = new XmlTextReader(sr)) 
    { 
    object entityTarget = (object )dcs.ReadObject(xmltr); 
    xmltr.Close(); 
    return (T)entityTarget; 
    } 
    return null ; 
    } 
    


    What am i missing to sucessfully deserialize in a new object?

    TIA, Kees

     

    Tuesday, April 6, 2010 3:13 PM

Answers

  • Have you set the serialization mode to 'unidirectional' in the L2S designer (datacontext properties)?

    Also, which exception does it throw?


    Kristofer - Huagati Systems Co., Ltd.
    Cool tools for Linq-to-SQL and Entity Framework:
    huagati.com/dbmltools (add-in with new features for the L2S and EF designers in VS2008 and VS2010)
    huagati.com/L2SProfiler (Query profiler for Linq-to-SQL and LLBLGen Pro)
    Wednesday, April 7, 2010 4:12 AM
    Answerer
  • Hello,

    Thanks for using MSDN Linq to SQL forum!

    Based on my understanding, you want to update an entity and then attach back to the DataContext. Please correct me if I misunderstand you.

    Usually, if you attach back an entity without serializing/deserializing, you may encounter an exception like:
    "An attempt has been made to Attach or Add an entity that is not new, perhaps having been loaded from another DataContext.  This is not supported" Or it throws duplicate primary key exception. Here is a blog descibe the exception:
    http://blogs.microsoft.co.il/blogs/bursteg/archive/2007/11/21/linq-to-sql-table-lt-t-gt-detach-method-does-not-exist.aspx

    However, you could implement that without detach if you serialize and deserialize your entity, just create another instance of your DataContext. The code snippet for you to test looks like:

            public static void Test()
            {
                AdventureWorksDataContext db = new AdventureWorksDataContext();
                LinqToSql.Customer customer = db.Customers.First();
                
                MemoryStream testStream = new MemoryStream();
                testStream = Serialize(customer); // I used your Serialize method.
    
                Customer c = Deserialize<Customer>(testStream); // you deserialized method.
                AdventureWorksDataContext db2 = new AdventureWorksDataContext();
    
                db2.Customers.Attach(c);
    
                Console.WriteLine("it's done.");
            }
    
    

    Please have a try and let me know the result.
    Best regards


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    If you have any feedback, please tell us.
    Welcome to the All-In-One Code Framework!
    Friday, April 9, 2010 11:02 AM

All replies

  • Have you set the serialization mode to 'unidirectional' in the L2S designer (datacontext properties)?

    Also, which exception does it throw?


    Kristofer - Huagati Systems Co., Ltd.
    Cool tools for Linq-to-SQL and Entity Framework:
    huagati.com/dbmltools (add-in with new features for the L2S and EF designers in VS2008 and VS2010)
    huagati.com/L2SProfiler (Query profiler for Linq-to-SQL and LLBLGen Pro)
    Wednesday, April 7, 2010 4:12 AM
    Answerer
  • Hello,

    Thanks for using MSDN Linq to SQL forum!

    Based on my understanding, you want to update an entity and then attach back to the DataContext. Please correct me if I misunderstand you.

    Usually, if you attach back an entity without serializing/deserializing, you may encounter an exception like:
    "An attempt has been made to Attach or Add an entity that is not new, perhaps having been loaded from another DataContext.  This is not supported" Or it throws duplicate primary key exception. Here is a blog descibe the exception:
    http://blogs.microsoft.co.il/blogs/bursteg/archive/2007/11/21/linq-to-sql-table-lt-t-gt-detach-method-does-not-exist.aspx

    However, you could implement that without detach if you serialize and deserialize your entity, just create another instance of your DataContext. The code snippet for you to test looks like:

            public static void Test()
            {
                AdventureWorksDataContext db = new AdventureWorksDataContext();
                LinqToSql.Customer customer = db.Customers.First();
                
                MemoryStream testStream = new MemoryStream();
                testStream = Serialize(customer); // I used your Serialize method.
    
                Customer c = Deserialize<Customer>(testStream); // you deserialized method.
                AdventureWorksDataContext db2 = new AdventureWorksDataContext();
    
                db2.Customers.Attach(c);
    
                Console.WriteLine("it's done.");
            }
    
    

    Please have a try and let me know the result.
    Best regards


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    If you have any feedback, please tell us.
    Welcome to the All-In-One Code Framework!
    Friday, April 9, 2010 11:02 AM
  • This has worked.

     

    I've tried to run it on my context but this seems to give the same errors, till i changed the constructor of some objects. Here i added the default for entityRef and an initialized EntitySet. This gave the solution.

    Monday, April 12, 2010 1:55 PM