I have an M to N relationship on my Database. I took the sample Database "sakila" from the MySql page. I have set up my Model Objects as follows.
Film Table
public partial class Film
{
public Film()
{
FilmActor = new HashSet<FilmActor>();
FilmCategory = new HashSet<FilmCategory>();
Inventory = new HashSet<Inventory>();
}
//Some Properties
public ICollection<Inventory> Inventory { get; set; }
}
Linking Table Inventory
public partial class Inventory
{
public Inventory()
{
Rental = new HashSet<Rental>();
}
public int InventoryId { get; set; }
public short FilmId { get; set; }
public byte StoreId { get; set; }
public DateTimeOffset LastUpdate { get; set; }
public Film Film { get; set; }
public Store Store { get; set; }
public ICollection<Rental> Rental { get; set; }
}
Store Table
public partial class Store
{
public Store()
{
Customer = new HashSet<Customer>();
Inventory = new HashSet<Inventory>();
Staff = new HashSet<Staff>();
}
//More Properties
public ICollection<Inventory> Inventory { get; set; }
}
When I go to retrieve the Data through a Repository I get back a list of Store objects that has a list of Inventory that has a list of films that has a list of stores... In other words: store[2]->inventory[2270]->film->store[2]->inventory... ad infinitum.
So how do I make this stop when my Model gets to the film objects?
Entity Framework tracks your database objects by default when you query them. If you fetch child or parents objects in your query, either through lazy or eager loading, they will be "stored" on the (local) context.
This means that whenever you enter debug mode and hit a breakpoint, you can infinitely step through your object tree. At runtime, this has no real effect on performance.