Creating a new many to many table

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
12 years ago
I added a new table to the DB to indicate states where a customer is exempted from sales tax.

Customer_TaxExemptStates
Customer_Id int
StateProvince_Id int


Added this to Core.Domain.Customers.Customer:

public virtual ICollection<StateProvince> TaxExemptStates
        {
            get { return _taxExemptStates ?? (_taxExemptStates = new List<StateProvince>()); }
            protected set { _taxExemptStates = value; }
        }

Wired it up in the Map class:

            this.HasMany(c => c.TaxExemptStates)
                .WithMany()
                .Map(m => m.ToTable("Customer_TaxExemptStates"));


Then in a ITask I made, I'm trying to add states to the list:


StateProvince state = _stateProvinceService.GetStateProvinceByAbbreviation(stateCode);

customer.TaxExemptStates.Add(state);
_customerService.UpdateCustomer(customer);


I get an exception on the .Add line saying that the two objects belong to different contexts.  Any ideas?  If I change it to adding a new StateProvince to the list with the fields from state, it works but I get a new StateProvince in my table.
12 years ago
How have added this new table? Is it in the core solution or implemented as a plugin? I presume the second approach. If yes, then it's not possible to "join" entities from different contexts - https://www.nopcommerce.com/boards/t/13958/extending-nop-data-model.aspx
12 years ago
I added the linking table in the core but it's not really a new entity.  It's just a many to many table to link the Customer and StateProvince entities.
12 years ago
It's the same idea as CustomerRoles.  I'm looking at this from the CustomerController in Admin and it seems like the same thing I'm trying to do but mine doesn't work:


//customer roles
                    var allCustomerRoles = _customerService.GetAllCustomerRoles(true);
                    foreach (var customerRole in allCustomerRoles)
                    {
                        if (model.SelectedCustomerRoleIds != null && model.SelectedCustomerRoleIds.Contains(customerRole.Id))
                        {
                            //new role
                            if (customer.CustomerRoles.Where(cr => cr.Id == customerRole.Id).Count() == 0)
                                customer.CustomerRoles.Add(customerRole);
                        }
                        else
                        {
                            //removed role
                            if (customer.CustomerRoles.Where(cr => cr.Id == customerRole.Id).Count() > 0)
                                customer.CustomerRoles.Remove(customerRole);
                        }
                    }
                    _customerService.UpdateCustomer(customer);
12 years ago
It worked if I added a repository of StateProvince to the CustomerService and pulled my state from there.  If I use two different services, do they have two different Contexts?  What if I inject the State service into CustomerService and pull a state that way?  I'll try it out.
12 years ago
and that does work.  It seems odd but I guess it's an acceptable workaround.
12 years ago
AndyMcKenna wrote:
If I use two different services, do they have two different Contexts?

No, the context is instantiated per request. I don't know why you're getting the error. Really weird.
12 years ago
I'm running this from an ITask though so I don't necessarily have a request.  I think that is what was throwing it off.
12 years ago
Sorry to jump in guy's but I thought this would be the a good start to ask. I'm aware you are very busy.

My in house db runs of Analysis codes for its category's. Eg. BY01 Baby Skin Care.

So when a new line is uploaded it is mapped to these codes.

I'm slightly confused on where to connect it. I don't need the codes to display on the front end I just need them to connect in the Db so when my Business Colegues upload new lines of stock it maps to its category automatically.

Mirroring the field in the local Db

it is

[ANAL CODE] varchar(8) allow nulls.

I'm just hoping you could help again. Should I place it in the Category or product table in the NopCommerce Db..

All help highly regarded.

Richard
12 years ago
Seems like it would only make sense on the Category table.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.