class Program
 {

 static void Main(string[] args)
 {
 DbInterception.Add(new LogInterceptor());
 Console.OutputEncoding = System.Text.Encoding.UTF8;// to show romanian alphabet characters

using (var ctx = new MainEntities())
 {
// Eagerly load all data for customers.
 var customers = ctx.Customers.Include(i => i.Orders.Select( j => j.OrderItems.Select( k => k.Product ) ) );

// TODO: how does one load only the first customer and its related data? Is it worth trying // to do that?
 var customer = customers.First(i => i.Id == 1);
 Console.WriteLine("Customer data");
 Console.WriteLine($"{customer.Id},{customer.FirstName},{customer.LastName}");
 Console.WriteLine("Orders");
 foreach (var order in customer.Orders)
 {
 Console.WriteLine($"{order.Id}");
 foreach (var orderItem in order.OrderItems)
 {
 Console.WriteLine($"{orderItem.Product.Name},{orderItem.Count} unități,{orderItem.UnitPrice} preț unitar,total: {orderItem.Count*orderItem.UnitPrice} RON ");
 }
 }

}
 }

class LogInterceptor : IDbCommandInterceptor
{
public void NonQueryExecuted(DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
{

}

public void NonQueryExecuting(DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
{

}

public void ReaderExecuted(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext)
{

}

public void ReaderExecuting(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext)
{
Debug.Print($"Executing sql:{command.CommandText}");
}

public void ScalarExecuted(DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
{

}

public void ScalarExecuting(DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
{

}
}
Advertisements