using System.Data.Entity;
using System.Collections.Generic;
public static void Main()
using (var context = new EntityContext())
DateTime oldDate = DateTime.Now.Subtract(new TimeSpan(noOfDays, 0, 0, 0, 0));
var invoices = context.Invoices
.Where(i => i.Date > oldDate)
public static void InsertData()
using (var context = new EntityContext())
context.BulkInsert(CustomerData());
context.BulkInsert(InvoiceData());
context.BulkInsert(ItemData());
public static List<Customer> CustomerData()
List<Customer> list = new List<Customer>()
new Customer() { Name ="John"},
new Customer() { Name ="Andy"},
new Customer() { Name ="Mark"}
public static List<Invoice> InvoiceData()
List<Invoice> list = new List<Invoice>()
new Invoice() { Date = DateTime.Now.AddDays(-107), CustomerID = 1},
new Invoice() { Date = DateTime.Now.AddDays(-5), CustomerID = 1},
new Invoice() { Date = DateTime.Now.AddDays(-3), CustomerID = 1},
new Invoice() { Date = DateTime.Now.AddDays(-105), CustomerID = 2},
new Invoice() { Date = DateTime.Now.AddDays(-59), CustomerID = 3},
new Invoice() { Date = DateTime.Now.AddDays(-25), CustomerID = 3},
public static List<Item> ItemData()
List<Item> list = new List<Item>()
new Item() { Name = "Item 1", InvoiceID = 1},
new Item() { Name = "Item 2", InvoiceID = 1},
new Item() { Name = "Item 3", InvoiceID = 1},
new Item() { Name = "Item 10", InvoiceID = 2},
new Item() { Name = "Item 5", InvoiceID = 2},
new Item() { Name = "Item 11", InvoiceID = 3},
new Item() { Name = "Item 21", InvoiceID = 3},
new Item() { Name = "Item 3", InvoiceID = 3},
new Item() { Name = "Item 5", InvoiceID = 4},
new Item() { Name = "Item 4", InvoiceID = 4},
new Item() { Name = "Item 15", InvoiceID = 4},
new Item() { Name = "Item 35", InvoiceID = 4},
new Item() { Name = "Item 1", InvoiceID = 5},
new Item() { Name = "Item 4", InvoiceID = 5},
new Item() { Name = "Item 3", InvoiceID = 6},
new Item() { Name = "Item 2", InvoiceID = 6},
new Item() { Name = "Item 6", InvoiceID = 6},
new Item() { Name = "Item 11", InvoiceID = 6},
new Item() { Name = "Item 23", InvoiceID = 6},
new Item() { Name = "Item 1", InvoiceID = 6},
new Item() { Name = "Item 4", InvoiceID = 6},
new Item() { Name = "Item 20", InvoiceID = 6},
public static void DisplayData(List<Invoice> list)
foreach(var invoice in list)
Console.WriteLine(invoice.Date);
foreach(var item in invoice.Items)
Console.WriteLine("\t" + item.Name);
public class EntityContext : DbContext
public EntityContext() : base(FiddleHelper.GetConnectionStringSqlServer())
public DbSet<Customer> Customers { get; set; }
public DbSet<Invoice> Invoices { get; set; }
public int CustomerID { get; set; }
public string Name { get; set; }
public virtual List<Invoice> Invoices { get; set; }
public int InvoiceID { get; set; }
public DateTime Date { get; set; }
public int CustomerID { get; set; }
public virtual Customer Customer { get; set; }
public virtual ICollection<Item> Items { get; set; }
public int ItemID { get; set; }
public string Name { get; set; }
public int InvoiceID { get; set; }
public virtual Invoice Invoice { get; set; }