I’ve got a very simple data structure. I’m using SQLExpress with Linq2SQL and vb.net
**ParentClass**
parentId
name
**ChildClass**
childId
name
parentId (foreign key to parent table)
The dbml reflects these two classes, and has a oneToMany association.
So far, so good.
In my code, i’m trying to get the value as follows
Dim count as Integer = Parent.ChildClasses.Count
(the answer is 10, btw). It works at first. I add five Child records. The count should now be 15, but its still reading 10. If i rebuild the solution and rerun the application, it correctly shows 15 – at least until i start adding more records.
I’m pretty sure this is a pretty n00bish mistake I’m making somewhere. Is it a function of lazy loading or am I barking up the wrong tree?
Your Variable won’t magically change because the number of items in the context changed.
Lazy Loading refers to the fact that the query is evaluated unit it is needed, but since you are assigning a variable that value it is needed and evaluated, thus checking the variable doesn’t cause the query to run again.
A good way of doing this would be just to make a read only property and just call the Count each time you need it