Many to many update problem on web

Dec 24, 2014 at 10:43 AM
Hi all and Happy Holidays!
I am trying to play with many to many update (Customer, CustomerDemographics and CustomerCustomerDemo tables)
I added a record into table CustomerDemographics: CustomerTypeID: 'a', CustomerDesc: 'test'

I tested in Test project and everything is fine.
Here's the code
[TestMethod]
        public void UpdateManyToManyTest()
        {
            using (IDataContextAsync context = new NorthwindContext())
            using(IUnitOfWorkAsync unitOfWork = new UnitOfWork(context))
            {
                IRepositoryAsync<Customer> customerRepository = new Repository<Customer>(context, unitOfWork);
                IRepositoryAsync<CustomerDemographic> customerDemoRepository = new Repository<CustomerDemographic>(context, unitOfWork);
                IService<Customer> customerService = new CustomerService(customerRepository);
                IService<CustomerDemographic> customerDemoService = new CustomerDemographicService(customerDemoRepository);
                var customer = customerService.Find("ALFKI");
                customer.City = "Berlinn";
                var customerDemo = customerDemoService.Find("a");
                customer.CustomerDemographics.Add(customerDemo);
                
                customer.ObjectState = ObjectState.Modified;
                customerRepository.InsertOrUpdateGraph(customer);
                unitOfWork.SaveChanges();
            }

            //Test
            using (IDataContextAsync context = new NorthwindContext())
            using (IUnitOfWorkAsync unitOfWork = new UnitOfWork(context))
            {
                IRepositoryAsync<Customer> customerRepository = new Repository<Customer>(context, unitOfWork);
                Customer customerToTest = null;
                customerToTest = customerRepository.Query(x => x.CustomerID == "ALFKI").Include(x=>x.CustomerDemographics).Select().SingleOrDefault();
                Assert.AreEqual(customerToTest.City, "Berlinn");
                Assert.IsTrue(customerToTest.CustomerDemographics.Count == 1);
            }
        }
However, I failed when runs on Web, there is no record inserted into CustomerCustomerDemo
private readonly ICustomerDemographicService _customerDemographicService;
public CustomerController(
            IUnitOfWorkAsync unitOfWorkAsync,
            ICustomerService customerService, ICustomerDemographicService customerDemographicService)
        {
            _unitOfWorkAsync = unitOfWorkAsync;
            _customerService = customerService;
            _customerDemographicService = customerDemographicService;
        }

public async Task<IHttpActionResult> Put(string key, Customer customer)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (key != customer.CustomerID)
            {
                return BadRequest();
            }
            var customerDemo = _customerDemographicService.Find("a");
            customer.CustomerDemographics.Add(customerDemo);
            customer.ObjectState = ObjectState.Modified;
            try
            {
                _customerService.InsertOrUpdateGraph(customer);
                await _unitOfWorkAsync.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CustomerExists(key))
                {
                    return NotFound();
                }
                throw;
            }
            catch (Exception ex) { }
            return Updated(customer);
        }
Thanks
Coordinator
Dec 29, 2014 at 10:25 AM
Have you checked and confirm that you DI/IoC configuration correctly? Make sure you IDataContext is setup to have Singleton like behavior for the lifecycle of your Http Request.
Marked as answer by lelong37 on 12/29/2014 at 2:25 AM
Dec 29, 2014 at 8:44 PM
Here's code for DI/IoC
container
                .RegisterType<IDataContextAsync, NorthwindContext>(new PerRequestLifetimeManager())
                .RegisterType<IUnitOfWorkAsync, UnitOfWork>(new PerRequestLifetimeManager())
                .RegisterType<IRepositoryAsync<Customer>, Repository<Customer>>()
                .RegisterType<IRepositoryAsync<CustomerDemographic>, Repository<CustomerDemographic>>()
                .RegisterType<ICustomerService, CustomerService>()
                .RegisterType<ICustomerDemographicService, CustomerDemographicService>()
What changes should I do?

Thanks
Coordinator
Dec 30, 2014 at 5:26 PM
Edited Dec 30, 2014 at 5:29 PM
There's not enough context here to decipher what the issue is, is this source code online somewhere e.g. GitHub, TFS? You can request an online support session, first 15 minutes is free given we can solve it in that time frame.
Dec 30, 2014 at 8:46 PM
Thanks Long,
I have been playing on your sample project. You can get it from my public one drive (Sample.zip):
https://onedrive.live.com/?cid=D3BB9A9710D53E80&id=D3BB9A9710D53E80%21107