Database Reference
In-Depth Information
and product B is related to product A, our application would get trapped in the recursion. To detect cycles, we use a
Dictionary<> to help prune off paths that we have already traversed.
Listing 6-7. Forming the Transitive Closure of the “Related Products” Relationship
static void RunExample2()
{
using (var context = new EF6RecipesContext())
{
var product1 = new Product { Name = "Pole", Price = 12.97M };
var product2 = new Product { Name = "Tent", Price = 199.95M };
var product3 = new Product { Name = "Ground Cover", Price = 29.95M };
product2.RelatedProducts.Add(product3);
product1.RelatedProducts.Add(product2);
context.Products.Add(product1);
context.SaveChanges();
}
using (var context = new EF6RecipesContext())
{
var product1 = context.Products.First(p => p.Name == "Pole");
Dictionary<int, Product> t = new Dictionary<int, Product>();
GetRelated(context, product1, t);
Console.WriteLine("Products related to {0}", product1.Name);
foreach (var key in t.Keys)
{
Console.WriteLine("\t{0}", t[key].Name);
}
}
}
static void GetRelated(DbContext context, Product p, Dictionary<int, Product> t)
{
context.Entry(p).Collection(ep => ep.RelatedProducts).Load();
foreach (var relatedProduct in p.RelatedProducts)
{
if (!t.ContainsKey(relatedProduct.ProductId))
{
t.Add(relatedProduct.ProductId, relatedProduct);
GetRelated(context, relatedProduct, t);
}
}
context.Entry(p).Collection(ep => ep.OtherRelatedProducts).Load();
foreach (var otherRelated in p.OtherRelatedProducts)
{
if (!t.ContainsKey(otherRelated.ProductId))
{
t.Add(otherRelated.ProductId, otherRelated);
GetRelated(context, otherRelated, t);
}
}
}
Search WWH ::




Custom Search