Linq To Umbraco 4.5.2 - Exceptions with AssociationTree<>
Been playing around with Umbraco Linq in 4.5.2 and am impressed with the code-gen and the concept. It's going to be immense! But I've (inevitably) got a problem... I created two classes (interfaces and concrete) using the export function on DocTypes.
All works well when enumerating a document type. For instance, this code works great:
using (MyUmbracoDataContext uContext = new MyUmbraco.MyUmbracoDataContext()) { foreach (var item in uContext.Articles) { Response.Write(item.NodeName + "<br />"); } }
However, if I try and access the type's children then I always get an exception. Both the following statements will cause a FormatException:
foreach (var item in uContext.Articles) { int count = item.Articles.Count(); int children = item.Children.Count(); }
The exception that is raised is:
[FormatException: Input string was not in a correct format.]
System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) +7471479
System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info) +119
System.String.System.IConvertible.ToInt32(IFormatProvider provider) +46
System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider) +373
System.Convert.ChangeType(Object value, Type conversionType) +32
umbraco.Linq.Core.Node.NodeDataProvider.LoadFromXml(XElement xml, T node) +2415
umbraco.Linq.Core.Node.<DynamicNodeCreation>d__6.MoveNext() +468
System.Linq.WhereEnumerableIterator`1.MoveNext() +191
System.Linq.<CastIterator>d__aa`1.MoveNext() +181
System.Linq.Enumerable.Count(IEnumerable`1 source) +213
umbraco_LinqTest.Page_Load(Object sender, EventArgs e) in d:\xxx\xxxx\WWW\umbraco\LinqTest.aspx.cs:17
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
System.Web.UI.Control.OnLoad(EventArgs e) +99
System.Web.UI.Control.LoadRecursive() +50
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627
This happens for all AssociationTree<> types for every document type, not just Article.
Thanks, Eduardo, but Count() returns an Int32 so doesn't require parsing. But it's not the Count() part that causes the exception, it is the access to the collection. Getting the Count() was just an example of making Linq enumerate the child collection. For instance, the following lines will also cause the exception:
foreach (var child in item.Children) {
}
var articles = item.Articles.ToList();
Basically, as soon as the children are enumerated in any way the exception is raised.
The problem is that one of the Int-based properties has a value which is non-Int in the XML cache.
When LINQ to Umbraco is setting property values it does a few things:
- Checks if the property type is a value type (such as int)
- if the value in the XML is null (or empty) and the value type is Nullable<T> assign null to it
- if the value in the XML isn't null convert the value into an int and then set it to the property (or Nullable<T>.Value)
So the error is happening when it converts the value into an int to then set onto the property. Check the XML and ensure that all the data is either blank or a valid number
Hi Slace. Thanks for your input. I'd guessed it was probably something like this, the problem was identifying which of the many doc types was at fault! I eventually managed to identify the one and changed the Int32's to Strings and this indeed fixed my problem.
I can totally understand that automated code-generation can't always get this right, but wonder if it would be possible to include more diagnostics to help find any properties that fail conversion? This would be handy with complex, real-world sites.
But let me also say that even as it currently is, Linq to Umbraco is very impressive - so many thanks for all your hard work.
So to summarise the linq2umbraco generator doesnt always generate the right stuff?
I'm having the same kind of problems. In my case I have some custom datatypes that are set to string, but linq thinks that they are Int32's. I dont know how to change this.
Should I just change the linq cs file manually? which is kind of dorky, right...
Linq To Umbraco 4.5.2 - Exceptions with AssociationTree<>
Been playing around with Umbraco Linq in 4.5.2 and am impressed with the code-gen and the concept. It's going to be immense! But I've (inevitably) got a problem... I created two classes (interfaces and concrete) using the export function on DocTypes.
All works well when enumerating a document type. For instance, this code works great:
However, if I try and access the type's children then I always get an exception. Both the following statements will cause a FormatException:
The exception that is raised is:
This happens for all AssociationTree<> types for every document type, not just Article.
The generated IMaster interface looks like this:
And the IArticle interface looks like this:
Any help appreciated! If you need examples of any XML or whatever I can supply them. Thanks.
Hi Dan Diplo,
HTH
Sincere regards,
Eduardo
Thanks, Eduardo, but Count() returns an Int32 so doesn't require parsing. But it's not the Count() part that causes the exception, it is the access to the collection. Getting the Count() was just an example of making Linq enumerate the child collection. For instance, the following lines will also cause the exception:
Basically, as soon as the children are enumerated in any way the exception is raised.
Hi Dan,
You are right.
Put the code inside a try catch.
Check if child is not null before accesing it.
HTH
Sincere regards,
Eduardo
The problem is that one of the Int-based properties has a value which is non-Int in the XML cache.
When LINQ to Umbraco is setting property values it does a few things:
- Checks if the property type is a value type (such as int)
- if the value in the XML is null (or empty) and the value type is Nullable<T> assign null to it
- if the value in the XML isn't null convert the value into an int and then set it to the property (or Nullable<T>.Value)
So the error is happening when it converts the value into an int to then set onto the property. Check the XML and ensure that all the data is either blank or a valid number
Hi Slace. Thanks for your input. I'd guessed it was probably something like this, the problem was identifying which of the many doc types was at fault! I eventually managed to identify the one and changed the Int32's to Strings and this indeed fixed my problem.
I can totally understand that automated code-generation can't always get this right, but wonder if it would be possible to include more diagnostics to help find any properties that fail conversion? This would be handy with complex, real-world sites.
But let me also say that even as it currently is, Linq to Umbraco is very impressive - so many thanks for all your hard work.
Hi people,
So to summarise the linq2umbraco generator doesnt always generate the right stuff?
I'm having the same kind of problems. In my case I have some custom datatypes that are set to string, but linq thinks that they are Int32's. I dont know how to change this.
Should I just change the linq cs file manually? which is kind of dorky, right...
Do you have an idea slace?
Martin
is working on a reply...