UserService.CreateWithIdentity Creates User with Dirty AllowedSections and throws InvalidCastException
I'm trying to create a new user programmatically in Umbraco 7.2.1.
var userService = ApplicationContext.Current.Services.UserService;
var user = userService.GetByUsername(username);
if (user == null)
{
userService.CreateWithIdentity(username, username + "@domain.com", "", "UserType");
}
When I run this code with a new username, I get the following exception.
System.InvalidCastException was caught
HResult=-2147467262
Message=Object cannot be cast from DBNull to other types.
Source=mscorlib
StackTrace:
at System.DBNull.System.IConvertible.ToInt32(IFormatProvider provider)
at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
at System.Convert.ChangeType(Object value, Type conversionType)
at Umbraco.Core.Persistence.Database.PocoColumn.ChangeType(Object val)
at Umbraco.Core.Persistence.Database.Insert(String tableName, String primaryKeyName, Boolean autoIncrement, Object poco)
at Umbraco.Core.Persistence.Database.Insert(Object poco)
at Umbraco.Core.Persistence.Repositories.UserRepository.PersistNewItem(IUser entity)
at Umbraco.Core.Persistence.Repositories.RepositoryBase`2.PersistNewItem(IEntity entity)
at Umbraco.Core.Persistence.UnitOfWork.PetaPocoUnitOfWork.Commit(Action`1 transactionCompleting)
at Umbraco.Core.Persistence.UnitOfWork.PetaPocoUnitOfWork.Commit()
at Umbraco.Core.Services.UserService.CreateUserWithIdentity(String username, String email, String passwordValue, IUserType userType)
at Umbraco.Core.Services.UserService.Umbraco.Core.Services.IMembershipMemberService<Umbraco.Core.Models.Membership.IUser>.CreateWithIdentity(String username, String email, String passwordValue, String memberTypeAlias)
I dug through the source on github, and the only Database.Insert method I can find in the PersistNewItem method is adding user2app (e.g. AddedSections) data in UserRepository: 219.
I'm not sure why a newly created User would have any AddedSections, and where the DBNull could be coming from.
------------UPDATE--------------
When the UserRepository tries to save the new user2app rows, the UmbracoDatabase class is getting DBNull when it attempts to query for the id of the new user2app row.
I found the line that's trying to convert to the DBNull to an Int in the Database:1353. I'm running SQL Server, so it looks like the command that gets the DBNull id is Database:1281. I ran it in SQL Management Studio, and it does return NULL, though I may need to run other commands before that one to get anything meaningful.
I'm able to reproduce this issue on a fresh Umbraco installation if it's pointed to my old database, and my current project can create users if it's pointed to a new databes. So the problem is somewhere in my database configuration.
The id column in umbracoUser was missing the identity attribute + the constraints needed to detect that issue. I had to go through and readd those constraints and then create a new id column that had the identity attribute.
The DB got into this state because we imported data from another environment in a dumb way, and is 100% not a problem with Umbraco. Hopefully this will help someone if they run into a similar issue.
UserService.CreateWithIdentity Creates User with Dirty AllowedSections and throws InvalidCastException
I'm trying to create a new user programmatically in Umbraco 7.2.1.
When I run this code with a new username, I get the following exception.
I dug through the source on github, and the only Database.Insert method I can find in the PersistNewItem method is adding user2app (e.g. AddedSections) data in UserRepository: 219.
I'm not sure why a newly created User would have any AddedSections, and where the DBNull could be coming from.
------------UPDATE--------------
When the UserRepository tries to save the new user2app rows, the UmbracoDatabase class is getting DBNull when it attempts to query for the id of the new user2app row.
I found the line that's trying to convert to the DBNull to an Int in the Database:1353. I'm running SQL Server, so it looks like the command that gets the DBNull id is Database:1281. I ran it in SQL Management Studio, and it does return NULL, though I may need to run other commands before that one to get anything meaningful.
Thanks in advance for any insight or suggestions.
Update
I'm able to reproduce this issue on a fresh Umbraco installation if it's pointed to my old database, and my current project can create users if it's pointed to a new databes. So the problem is somewhere in my database configuration.
I fixed it!
The id column in umbracoUser was missing the identity attribute + the constraints needed to detect that issue. I had to go through and readd those constraints and then create a new id column that had the identity attribute.
The DB got into this state because we imported data from another environment in a dumb way, and is 100% not a problem with Umbraco. Hopefully this will help someone if they run into a similar issue.
is working on a reply...