Bug 25193

Summary: XElement casting from SQL Server XML field fails in Linq query
Product: [Mono] Class Libraries Reporter: Alex Diaz <alejandro.penate>
Component: System.DataAssignee: Bugzilla <bugzilla>
Status: NEW ---    
Severity: normal CC: mono-bugs+mono
Priority: ---    
Version: 3.8.0   
Target Milestone: Untriaged   
Hardware: Macintosh   
OS: Mac OS   
Tags: Is this bug a regression?: ---
Last known good build:

Description Alex Diaz 2014-12-09 11:44:31 UTC
I am running the same code in Mono 3.10 for Mac and .NET Framework 4.5. IT works in .NET but in Xamarin it fails to process a Linq query.

//Save to database
				qry = from prod in dc.products where prod.id == id select prod;
				if (qry.Count() > 0)
				{
					var item = qry.Single();
					item.name = tree.Root.name;
					MemoryStream stream = tree.Serialize ();
					item.product1 = stream.ToArray();
					item.modified_by = user;
					item.modified = DateTime.Now;
					item.size = (int)stream.Length;
                                        string xml = tree.SerializeToString();
                                        item.product_xml = XElement.Parse(xml);
					dc.SubmitChanges();
				}
				else
				{
					product product = new product();
					MemoryStream stream = tree.Serialize ();
					product.product1 = stream.ToArray();
					product.name = tree.Root.name;
					product.created_by = user;
					product.modified_by = user;
					product.created = DateTime.Now;
					product.modified = DateTime.Now;
					product.store_id = store_id;
					product.size = (int)stream.Length;
                                        string xml = tree.SerializeToString();
                                        product.product_xml = XElement.Parse(xml);
					product.active = false;
					dc.products.InsertOnSubmit(product);
					dc.SubmitChanges();
				}


System.InvalidCastException
Cannot cast from source type to destination type.

Description: HTTP 500.Error processing request.
Details: Non-web exception. Exception origin (name of application or object): System.Data.Linq.
Exception stack trace:

at (wrapper dynamic-method) object.lambda_method (System.Runtime.CompilerServices.Closure,System.Data.IDataRecord,DbLinq.Data.Linq.Mapping.MappingContext) <IL 0x000af, 0x00487>
at DbLinq.Data.Linq.Sugar.Implementation.QueryRunner.Select<OnlinePriceSystem.Models.product> (DbLinq.Data.Linq.Sugar.SelectQuery) <0x0029a>
at DbLinq.Data.Linq.Sugar.Implementation.QueryRunner.SelectSingle<OnlinePriceSystem.Models.product> (DbLinq.Data.Linq.Sugar.SelectQuery,bool) <0x00090>
at DbLinq.Data.Linq.Sugar.Implementation.QueryRunner.SelectScalar<OnlinePriceSystem.Models.product> (DbLinq.Data.Linq.Sugar.SelectQuery) <0x00284>
at DbLinq.Data.Linq.Implementation.QueryProvider`1<OnlinePriceSystem.Models.product>.Execute<OnlinePriceSystem.Models.product> (System.Linq.Expressions.Expression) <0x0009c>
at System.Linq.Queryable.Single<OnlinePriceSystem.Models.product> (System.Linq.IQueryable`1<OnlinePriceSystem.Models.product>) <0x00184>
at OnlinePriceSystem.Controllers.TreeViewController.SaveProduct (int) [0x00369] in /Users/alex/online-price-system/OPS/Controllers/TreeViewController.cs:671
at (wrapper dynamic-method) object.lambda_method (System.Runtime.CompilerServices.Closure,System.Web.Mvc.ControllerBase,object[]) <IL 0x0000e, 0x00077>
at System.Web.Mvc.ActionMethodDispatcher.Execute (System.Web.Mvc.ControllerBase,object[]) <IL 0x00008, 0x00031>
at System.Web.Mvc.ReflectedActionDescriptor.Execute (System.Web.Mvc.ControllerContext,System.Collections.Generic.IDictionary`2<string, object>) <IL 0x0007a, 0x002d3>
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod (System.Web.Mvc.ControllerContext,System.Web.Mvc.ActionDescriptor,System.Collections.Generic.IDictionary`2<string, object>) <IL 0x00003, 0x00033>
at System.Web.Mvc.Async.AsyncControllerActionInvoker.InvokeSynchronousActionMethod (System.Web.Mvc.ControllerContext,System.Web.Mvc.ActionDescriptor,System.Collections.Generic.IDictionary`2<string, object>) <IL 0x00004, 0x00034>
at System.Web.Mvc.Async.AsyncControllerActionInvoker/<>c__DisplayClass42.<BeginInvokeSynchronousActionMethod>b__41 () <IL 0x00018, 0x00043>
at System.Web.Mvc.Async.AsyncResultWrapper/<>c__DisplayClass8`1<System.Web.Mvc.ActionResult>.<BeginSynchronous>b__7 (System.IAsyncResult) <0x00029>
at System.Web.Mvc.Async.AsyncResultWrapper/WrappedAsyncResult`1<System.Web.Mvc.ActionResult>.End () <0x000c4>
at System.Web.Mvc.Async.AsyncResultWrapper.End<System.Web.Mvc.ActionResult> (System.IAsyncResult,object) <0x00067>
at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod (System.IAsyncResult) <IL 0x00006, 0x00033>
at System.Web.Mvc.Async.AsyncControllerActionInvoker/<>c__DisplayClass37/<>c__DisplayClass39.<BeginInvokeActionMethodWithFilters>b__33 () <IL 0x00030, 0x000a2>
at System.Web.Mvc.Async.AsyncControllerActionInvoker/<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49 () <IL 0x00008, 0x0004c>
Comment 1 Alex Diaz 2014-12-09 21:51:03 UTC
It seems that the problem is in the method SubmitChanges() of class DataContext