Darren Wainwright
Darren Wainwright

Reputation: 30727

ASP.Net Identity not creating user

I've used ASP.Net Identity a couple of times now. On a new project I seem to be having an issue creating a user.

When calling _userManager.Create() I get the following error.

  The string '{ Name: IX_UserId, Order: 0 }' was not 
  in the expected format to be deserialized by the 
  IndexAnnotationSerializer. Serialized values are expected to have 
  the format '{ Name: 'MyIndex', Order: 7, IsClustered: True, 
               sUnique: False } { } { Name: 'MyOtherIndex' }'.

I've tried using the following DbContext, which - apart from the class name - is identical to the DbContext i have in another project, that works

public partial class ISIdentityDbContext : IdentityDbContext<IdentityUser>
    {

        public ISIdentityDbContext()
            : base("ISIdentityDbContext")
        { }

        public DbSet<ApplicationUserUserInfoMap> ApplicationUserUserInfoMap { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            // asp.net identity - call the tables something else..
            modelBuilder.Entity<IdentityRole>().ToTable("ApplicationRoles");
            modelBuilder.Entity<IdentityUserClaim>().ToTable("ApplicationUserClaims");
            modelBuilder.Entity<IdentityUserLogin>().ToTable("ApplicationUserLogins");
            modelBuilder.Entity<IdentityUserRole>().ToTable("ApplicationUserRoles");
            modelBuilder.Entity<IdentityUser>().ToTable("ApplicationUser");  
        }              

    }

I have tried the following:

 using (ISIdentityDbContext context = new ISIdentityDbContext())
            {                
                _userManager = new UserManager<IdentityUser>(new UserStore<IdentityUser>(context));
                IdentityUser user = new IdentityUser();
                user.UserName = "darren";
                _userManager.Create(user, "password");
            }

And also, the one I really need to get working as it's extending the ApplicationUser (IdentityUser)

using (ISIdentityDbContext context = new ISIdentityDbContext())
            {
                _userManager = new UserManager<LegacyApplicationUser>(new UserStore<LegacyApplicationUser>(context));
                ApplicationUserUserInfoMap map = new ApplicationUserUserInfoMap();
                map.UserGUID = "anIdFromAnotherTable";

                LegacyApplicationUser user = new LegacyApplicationUser();
                user.UserInfoMap = map;
                user.UserName = "darren";
                _userManager.Create(user, "password");
            }

Where my LegacyApplicationUser is:

   public class LegacyApplicationUser : IdentityUser
    {
        public virtual ApplicationUserUserInfoMap UserInfoMap { get; set; }
    }

    public class ApplicationUserUserInfoMap
    {
        public int ID { get; set; }
        public string UserGUID { get; set; }
    }

I'm totally stumped...no matter whether i rebuild my database to match the standard Identity users or use my extended version i keep getting the same exception shown at the top.

I've likely missed something, though can't figure what....

Any ideas?

Upvotes: 2

Views: 2662

Answers (2)

Darren Wainwright
Darren Wainwright

Reputation: 30727

Ok - I fixed it!

I was going to remove this question as, as it turns out, it's a very narrow question.

that said, I will leave it in here for anybody else struggling to get EF to play nice with a database that isn't all going through EF.

In our case we have a DB that won't be having EF built against it (it's a very old DB) - but some new parts will be EF'ed; the ASP.Net Identity parts.

It turns out my problem was actually with the __MigrationHistory table.

Once I added a DbInterceptor to my DbContext I could see the actual SQL causing the error.

I removed the entries in the _MigrationHistory table and it all worked.

Upvotes: 3

John Nicholas
John Nicholas

Reputation: 4836

I have had the same problem

I just create the user without a password then use the password hasher to select the user back out and store it again as a work around. It only fails when i set username and password - i met it in code seeding a database.

Upvotes: 0

Related Questions