I’m really digging the DDD Aggregate root design pattern and thought it would be good to create an example of using EF to pull a root from the persistence layer (database).

My database:

image

In my little database prjects have releases and stories. releases have sprints and stories and stories have tasks. My root is a project.

 

My model (EF)

image

A method for pulling the root:

Its important to note that child objects must be included in the query. ex: Include("Stories.Tasks") will get the tasks.

All the projects

Dim _entities As Entities.EntityFrameworkEntities = New Entities.EntityFrameworkEntitiesReturn
_entities.Projects().Include("Releases").Include("Stories").Include("Stories.Tasks")

A specific project.

Function GetById(ByVal Id As Integer) As Entities.Projects       
Return _entities.Projects().Include("Releases").Include("Stories").Include("Stories.Tasks").Where(Function(p) p.ProjectId = Id).FirstOrDefault
End Function