Linq2SQL counts caching?

I have a very simple data structure. I am using SQLExpress with Linq2SQL and vb.net

**ParentClass**
parentId
name


**ChildClass**
childId
name
parentId  (foreign key to parent table)

      

dbml reflects these two classes and has the oneToMany association.

So far so good.

In my code, I am trying to get the value like this

Dim count as Integer = Parent.ChildClasses.Count 

      

(Answer 10, by the way). It works first. I am adding five Child records. The score should now be 15, but it is still reading 10. If I rebuild the solution and rerun the application, it correctly displays 15 - at least until I start adding more entries.

I'm pretty sure this is quite a n00bish bug I am doing somewhere. Is this a lazy loading feature or am I barking in the wrong tree?

+2


source to share


1 answer


Your variable doesn't change magic because the number of items in the context has changed.

Lazy Loading refers to the fact that the request evaluates to one, which is needed, but since you are assigning a variable that needs and is evaluated, so checking that variable will not trigger the request again.



A good way to do this would be to just create a read-only property and just call Count every time you need it

Public ReadOnly Property Count As Integer
  Get
     Return Parent.ChildClasses.Count
  End Get
End Property

      

+1


source







All Articles