static void Main(string[] args)
        {
            using (BlauModel contextDB = new BlauModel())
            {
                contextDB.Database.CreateIfNotExists();
            }

            BlauModel context = new BlauModel();
            DBAddress a = context.Address.Where(x => x.Address1 == "509 Schaefer Street" && x.Postcode == "2641").FirstOrDefault();
            if (a == default(DBAddress))
            {
                a = new DBAddress();
                a.Address1 = "509 Schaefer Street";
                a.State = "NSW";
                a.Postcode = "2641";
                a.City = "Lavington";
                a.Country = context.Country.Where(x => x.countryCode == "AU").FirstOrDefault();
                context.Address.Add(a);
            }

            DBCustomer c = context.Customer.Where(x => x.CustomerNumber == "CN073396").FirstOrDefault();
            if (c == default(DBCustomer))
            {
                c = new DBCustomer();
                c.FirstName = "Michael";
                c.LastName = "Dann";
                c.Address = a;
                c.CustomerNumber = "CN073396";
                c.PhoneNumber = "0412032869";
                c.Title = "Mr";
                context.Customer.Add(c);
            }

            DateTime invoiceDate = new DateTime(2014, 3, 13, 13, 55, 41);
            string invoiceNumber = "SI1450869";
            DBCompany company = context.Company.Where(x => x.CompanyName == "Bright Life Australia").FirstOrDefault();
            DBInvoice invoice = context.Invoice.Where(x => x.InvoiceNumber == invoiceNumber && x.TimeCreated == invoiceDate).FirstOrDefault();
            if(invoice == default(DBInvoice))
            {
                invoice = new DBInvoice();
                invoice.TimeCreated = invoiceDate;
                invoice.InvoiceNumber = invoiceNumber;
                invoice.InvoiceStatus = 1;
                invoice.IPv4 = "127.0.0.1";
                invoice.Charge = 0;
                invoice.Company = company;
                invoice.Customer = c;
                context.Invoice.Add(invoice);
            }

            context.SaveChanges();
        }
        internal void SaveCustomerDetails(Customer customer)
        {
            using (BlauModel _dbcontext = BlauModel.GetContext())
            {
                DBCustomer dbCustomer = _dbcontext.Customer.Where(x => x.CustomerNumber == customer.CustomerNumber).FirstOrDefault();
                if (dbCustomer == default(DBCustomer))
                {
                    dbCustomer = new DBCustomer();
                }
                dbCustomer.FirstName = customer.Firstname;
                dbCustomer.LastName = customer.Lastname;
                dbCustomer.MiddleName = customer.Middlename;
                dbCustomer.Title = customer.Title;
                dbCustomer.PhoneNumber = customer.PhoneNumber;
                dbCustomer.EmailAddress = customer.EmailAddress;

                // Search for existing address first
                dbCustomer.Address = _dbcontext.Address.Where(x => x.Address1 == customer.Address1 && x.Postcode == customer.Postcode).FirstOrDefault();
                if (dbCustomer.Address == default(DBAddress))
                {
                    // New address
                    dbCustomer.Address = new DBAddress();
                }
                dbCustomer.Address.Address1 = customer.Address1;
                dbCustomer.Address.Address2 = customer.Address2;
                dbCustomer.Address.City = customer.City;
                dbCustomer.Address.State = customer.State;
                dbCustomer.Address.Postcode = customer.Postcode;
                dbCustomer.Address.Country = _dbcontext.Country.Where(x => x.countryName == customer.Country).FirstOrDefault();

                _dbcontext.SaveChanges();
            }
        }
 public Customer(DBCustomer customer)
 {
     loadData(customer);
 }
 /// <summary>
 /// Attempts to load the data for this customer into the database based on their customer number
 /// </summary>
 /// <returns>Whether the system was able to load the data or not</returns>
 private void loadData(DBCustomer customer)
 {
     if (customer != default(DBCustomer))
     {
         Address1 = customer.Address.Address1;
         Address2 = customer.Address.Address2;
         Title = customer.Title;
         Firstname = customer.FirstName;
         Lastname = customer.LastName;
         Middlename = customer.MiddleName;
         City = customer.Address.City;
         Postcode = customer.Address.Postcode;
         State = customer.Address.State;
         Country = customer.Address.Country.countryName;
         EmailAddress = customer.EmailAddress;
         PhoneNumber = customer.PhoneNumber;
     } else
     {
         throw new CustomerException("Customer details not found");
     }
 }