Users & Roles

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
13 年 前
What's the easiest way to assign a role dynamically (ie: via c# code)?
13 年 前
*bump*
13 年 前
Look at CustomerManager.AddCustomerToRole() method (NopSolutions.NopCommerce.BusinessLogic.CustomerManagement namespace)
13 年 前
Thank you kind sir, for answering my question, and for releasing this app.

For anybody else who may have this question in the future...

If you want to see the source, in the Solution Explorer, it's:

Libraries
Nop.BusinessLogic
Customer
CustomerManager.cs
13 年 前
Can you please help me  Mr. a.m?

When I try to add a user to staff role vi aadmin area,I got  object instance not set to instance of object.
when debugging :


        /// <summary>
        /// Adds a customer to role
        /// </summary>
        /// <param name="customerId">Customer identifier</param>
        /// <param name="customerRoleId">Customer role identifier</param>
        public static void AddCustomerToRole(int customerId, int customerRoleId)
        {
            var customer = GetCustomerById(customerId);
            if (customer == null)
                return;

            var customerRole = GetCustomerRoleById(customerRoleId);
            if (customerRole == null)
                return;

            var context = ObjectContextHelper.CurrentObjectContext;
            if (!context.IsAttached(customer))
                context.Customers.Attach(customer);
            if (!context.IsAttached(customerRole))
                context.CustomerRoles.Attach(customerRole);

            customer.NpCustomerRoles.Add(customerRole);<<<<<<<<<<<<<NpCustomerRoles is null
            context.SaveChanges();
        }

Am I missing something?
13 年 前
Add the following code before "customer.NpCustomerRoles.Add(customerRole)":
//ensure that navigation property is loaded
if (customer.NpCustomerRoles == null)
_context.LoadProperty(customer, c => c.NpCustomerRoles);
13 年 前
Thanks a lot a.m.

The code works perfectly ,I'd like to tell you that I used the same code in function
public static void RemoveCustomerFromRole(int customerId, int customerRoleId) as I faced the same error.
Now I can add,remove roles to customers perfectly.
13 年 前
Thanks a lot for your reply
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.