AndyMcKenna wrote:
Don't worry about the Id, just insert a customer and you will get the next Id.
hi Andy
I am worried about the id, I want to enter the guest customer at the first empty (deleted) id
public virtual Customer InsertGuestCustomer()
{
//custom code
var customerIds = from c in _customerRepository.Table
orderby c.Id
select c.Id;
int customerNextEmptyId = (
from n in customerIds
where !customerIds.Select(nu => nu).Contains(n + 1)
orderby n
select n + 1
).First();
//end of custom code
var customer = new Customer()
{
Id = customerNextEmptyId, //custom code
CustomerGuid = Guid.NewGuid(),
Active = true,
CreatedOnUtc = DateTime.UtcNow,
LastActivityDateUtc = DateTime.UtcNow,
};
//add to 'Guests' role
var guestRole = GetCustomerRoleBySystemName(SystemCustomerRoleNames.Guests);
if (guestRole == null)
throw new NopException("'Guests' role could not be loaded");
customer.CustomerRoles.Add(guestRole);
_customerRepository.Insert(customer);
return customer;
}
please see at the new Customer I force the Id
the thing is the entity passed to the Insert method get the forced id. I tried to override the Insert method, however I cant find a way to enter that record in a specific Id.
I SET IDENTITY_INSERT Customers ON with no luck.
thanks for your help
BTW, My point is to enter customers at the deleted spots and not increase the customers Id's