Creating a new many to many table

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
Hace 12 años
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.
Hace 12 años
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
Hace 12 años
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.
Hace 12 años
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);
Hace 12 años
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.
Hace 12 años
and that does work.  It seems odd but I guess it's an acceptable workaround.
Hace 12 años
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.
Hace 12 años
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.
Hace 12 años
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
Hace 12 años
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.