Yesterday (well, actually it was today, because it was from 23.30 till 1.30), I was working on my lunch presentation for the Developer Days 2004. Earlier that day I ran into a problem with the ObjectSpace class, trying to persist a new object. It was really a simple piece of code when I reduced the problem to:
SqlConnection conn = new SqlConnection(“DATA SOURCE=.;INTEGRATED SECURITY=SSPI“);
ObjectSpace os = new ObjectSpace(“MyMappingSchema.xml“, conn);
Posting p = new Posting();
p.Title = “Some title“;
p.Body = “This is the body“;
What I kept getting was a NullReferenceException on the last call. I can assure you that both the os variable and the p variable had non-null values. It took me till half past one in the night to find the cause:
<osd:ExtendedObjectSchema Name=”BlogObjectSchema” xmlns:osd=”http://schemas.microsoft.com/data/2002/09/20/persistenceschema“>
<osd:Member Name=”Title” />
<osd:Member Name=”Id” Key=”true” KeyType=”AutoIncrement” />
<osd:Member Name=”Body” />
I’ve bolded the offending line. When I swapped that line with the Title member, everything worked like a charm. I think this is a bug in ObjectSpaces. Nowhere is it stated that the order of declarations is significant; at least, not that I could find.
Does anybody have any experience with this feature/bug?