2

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?

7
  • It appears to be complaining about an index on the Users table, have you added one? Commented Jul 8, 2014 at 16:06
  • I have tried it vanilla too, just using IdentityUser and get the same issue, so not sure how it's having an issue. Commented Jul 8, 2014 at 16:10
  • I assume you've tried with a totally fresh database? In other words, let EF create it all for you. Commented Jul 8, 2014 at 16:33
  • EF is creating the Identity stuff for me. I can't use a totally fresh DB in this case as we're extending an old legacy system. EF is only creating the Identity parts though. Commented Jul 8, 2014 at 16:40
  • Are you adding in an index of your own? IX_UserId appears to be an index on UserId column. Commented Jul 8, 2014 at 16:42

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

usually I am letting EF code first to generate tables. Then making corresponding changes on production and updating _MigrationHistory data with the generated one.
I would normally - though this DB is an old DB that isn't feasible to generate EF models against - essentially, doing a hybrid with EF and non EF code.
just to clarify. When EF doesn't find _MigrationHistory, it is doing lot of failback work, some DB quireis, exception cathcing and so on. You could simply let EF build an empty DB on first call. Then copy _MigrationHistory data to your normal DB. EF will be tricket to beleive that the DB is OK.
0

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.

1 Comment

I still get the same issue. This is so very frustrating. The error message doesn't even make sense!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.