示例#1
0
        public List <Customer> GetAll()
        {
            var customers = new List <Customer>();

            using (var context = new CustomerInfoContext())
            {
                customers = context.Customers.Include("Contacts").ToList();
            }

            return(customers);
        }
示例#2
0
        public void Delete(int id)
        {
            using (var context = new CustomerInfoContext())
            {
                var existingCustomer = context.Customers.FirstOrDefault(x => x.Id == id);

                if (existingCustomer != null)
                {
                    context.Customers.Remove(existingCustomer);
                    context.SaveChanges();
                }
            }
        }
示例#3
0
        public Customer AddOrUpdate(Customer customer)
        {
            using (var context = new CustomerInfoContext())
            {
                var existingCustomer = context.Customers.Include("Contacts").FirstOrDefault(x => x.Id == customer.Id);
                if (existingCustomer == null)
                {
                    context.Customers.Add(customer);
                    context.SaveChanges();
                }
                else
                {
                    var existingContactIds = existingCustomer.Contacts.Select(x => x.Id).ToList();
                    foreach (var id in existingContactIds)
                    {
                        var contact = context.Contacts.FirstOrDefault(x => x.Id == id);
                        if (contact != null)
                        {
                            context.Contacts.Remove(contact);
                        }
                    }

                    existingCustomer.CompanyName      = customer.CompanyName;
                    existingCustomer.WebsiteUrl       = customer.WebsiteUrl;
                    existingCustomer.Industry         = customer.Industry;
                    existingCustomer.EnterpriseClient = customer.EnterpriseClient;
                    existingCustomer.PrimaryContactId = customer.PrimaryContactId;

                    foreach (var contact in customer.Contacts)
                    {
                        contact.CustomerId = existingCustomer.Id;
                        context.Contacts.Add(contact);
                    }

                    context.SaveChanges();
                }
            }

            return(customer);
        }
 public CustomerIdentityController(CustomerInfoContext dbcontext, IConfiguration configuration)
 {
     db = dbcontext;
     this.configuration = configuration;
 }