I am using Entity Framework, POCO entities and repository pattern for data access. I have a repository class which returns me Iqueryable of a particular type.
In my database I have a many to many relationship table.
Tables are like TemplatesTable and UserTable and junction UserTemplate table
One user can have multiple templates. So there is a junction table for mapping userid and templateid. Now I have to display all the template data like name, other details for users.
What is the best way to do this in EF POCO way?
Right Now I am following this approach which is working but I understand it is not the best way.
var Tempaltes = TemplateRepository.All();
var templateforUser = UserTemplateRepository.where(UserId==1)
var userTemplates= from tmp in Templates
join tmpusr in templateforUser on tmp.TemplateId equals tmpusr.TemplateId
select tmp;
return userTemplates.ToList();
Please suggest what is the best way to carry out this.
The correct approach is to have
Templatesnavigation property on yourUserentity. Than you just need to use eager or lazy loading to fill navigation property and you never need to use anything like custom join.