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(); }
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.