Self Tracking Entity - Loading Navigation Property of a Navigation Property
I am curious what the best practice is for this situation in Entity Framework 4.0. I have the following Entity Map:
Entity A -> Entity B -> Entity C
Where Entity A contains a list of Entity B's which each contains a list of Entity C's. I am using self tracking entities and when I load Entity A I am doing something similar to this:
EntityA entityA = ctx.EntityA.Include("EntityB").Where(x => x.Id == id).SingleOrDefault();
When I attempted to do this:
EntityA entityA = ctx.EntityA.Include("EntityB").Include("EntityC").Where(x => x.Id == id).SingleOrDefault();
It fails because it cannot find the navigation property 'Entity C'. Is there a way to load the navigation properties? I will also need to be able to Track Changes, Accept Changes, and MarkAsDeleted for my various CRUD operation开发者_Python百科s.
As a side-question, is there a common Attribute used to identity a Navigation Property in Entity Framework? I was hoping to use reflection and recursion to identify all of my Navigation Properties.
Try this out. It loads the navigation property from the current point in the object graph:
EntityA entityA = ctx.EntityA.Include("EntityB.EntityC").Where(x => x.Id == id).SingleOrDefault();
If you used the self tracking entities T4 template to generate your entities, then it should automatically track changes throughout your object graph.
After the self tracking T4 template is added to your project you can open it up and modify the file and add a custom attribute into the section that generates the navigation properties.
精彩评论