Linq to Sql json Serialization insertAllOnSubmit
-
Tuesday, October 02, 2012 5:35 AM
I have a task event where you can create dependencies, when I create the first dependency I get one entry in the database, however when I create a second dependency, I get two entries, instead of just the second entry
This is what I get when I create first dependency
{"jsonData":[{"id":0,"Id":0,"From":31225,"To":31226,"Type":2,"Lag":0,"Cls":""}]}
This is what I get when I create second dependency
{"jsonData":[{"id":0,"Id":0,"From":31225,"To":31226,"Type":2,"Lag":0,"Cls":""},{"id":0,"Id":0,"From":31226,"To":31227,"Type":2,"Lag":0,"Cls":""}]}
This is what I get when I create third dependency
{"jsonData":[{"id":0,"Id":0,"From":31225,"To":31226,"Type":2,"Lag":0,"Cls":""},{"id":0,"Id":0,"From":31226,"To":31227,"Type":2,"Lag":0,"Cls":""},{"id":0,"Id":0,"From":31227,"To":31228,"Type":2,"Lag":0,"Cls":""},{"id":0,"Id":0,"From":31228,"To":31229,"Type":2,"Lag":0,"Cls":""}]}
This is what I get when I create fourth dependency
{"jsonData":[{"id":0,"Id":0,"From":31225,"To":31226,"Type":2,"Lag":0,"Cls":""},{"id":0,"Id":0,"From":31226,"To":31227,"Type":2,"Lag":0,"Cls":""},{"id":0,"Id":0,"From":31227,"To":31228,"Type":2,"Lag":0,"Cls":""},{"id":0,"Id":0,"From":31228,"To":31229,"Type":2,"Lag":0,"Cls":""},{"id":0,"Id":0,"From":31229,"To":31230,"Type":2,"Lag":0,"Cls":""}]}Obviously this creates a circular reference
{"Message":"A circular reference was detected while serializing an object of type \u0027Task\u0027.","StackTrace":" at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValueInternal(Object o, StringBuilder sb, Int32 depth
My Question Is Do I deserialize on the InsertAllOnSubmit to prevent these duplicate entries?
My Code is relatively simple and it seems like threre should be an easy solution, I am really confused, thanks for any help!
[WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public Object Create(Dependency[] jsonData) { GanttChartDataClassesDataContext _db = new GanttChartDataClassesDataContext(); foreach (Dependency vals in jsonData) { if (vals == null) { vals.Id = vals.Id; vals.To = vals.To; vals.From = vals.From; vals.Type = vals.Type; } } _db.Dependencies.InsertAllOnSubmit(jsonData); _db.SubmitChanges(ConflictMode.ContinueOnConflict); return jsonData; }
All Replies
-
Monday, October 08, 2012 8:08 PMModerator
Hi,
I'm not completely sure I understand what is going on here; but does this discussion help:
http://www.west-wind.com/weblog/posts/2007/Sep/02/LINQ-to-SQL-and-Serialization
Thanks,
Cathy Miller

