Question

I've generated data model(Entity framework) with foreign keys. It was data model generated from database. After that everything has worked, navigation and etc. Next I have generated a Self-Tracking entity. Unfortunately I lost navigation feature. For example this query is not working now:

    public int GetUserTagsNumber( SessionContainer inputData ) {
        return db.User_t.Single(x => x.pid == inputData.Pid).Tag_t.Count();
    }

I'm new in Self-tracking entities and wcf. So, my questions are:

Why the navigation is not working also on service side?

If it is ok, then for what the navigation properties are visible?

Was it helpful?

Solution

Your "model navigation" is called lazy loading and self tracking entities don't support lazy loading by design so you must use eager loading instead:

db.User_t.Include("Tag_t").Single(x => x.pid == inputData.Pid).Tag_t.Count();

but this is wrong way to do the query because you must load user and all tags from database just to count tags. What about using direct query to get just count from database?

db.Tag_t.Where(/* here put condition to find tags used by your user */).Count();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top