Identity icon indicating copy to clipboard operation
Identity copied to clipboard

Indentity To INT

Open GVX111 opened this issue 11 years ago • 5 comments

Application startup exception: System.Reflection.TargetInvocationException: Exce ption has been thrown by the target of an invocation. ---> System.ArgumentExcept ion: GenericArguments[0], 'ITN.TS.UserStore.Models.Account', on 'Microsoft.AspNe t.Identity.EntityFramework.UserStore3[TUser,TRole,TContext]' violates the const raint of type 'TUser'. ---> System.TypeLoadException: GenericArguments[0], 'ITN. TS.UserStore.Models.Account', on 'Microsoft.AspNet.Identity.EntityFramework.User Store4[TUser,TRole,TContext,TKey]' violates the constraint of type parameter 'T User'.

 
services.AddIdentity< Account, IdentityRole >()
               .AddEntityFrameworkStores< AccountContext, int >()
               .AddDefaultTokenProviders();
public class ApplicationUserLogin : IdentityUserLogin { }
    public class ApplicationUserRole : IdentityUserRole { }
    public class ApplicationUserClaim : IdentityUserClaim { }

    public sealed class ApplicationRole : IdentityRole
    {
        public ApplicationRole() { }
        public ApplicationRole(string name) { Name = name; }
    }

    public class ApplicationUserStore : UserStore
    {
        public ApplicationUserStore(AccountContext context) : base(context) { }
    }

    public class ApplicationRoleStore : RoleStore
    {
        public ApplicationRoleStore(AccountContext context) : base(context) { }
    }

    public class ApplicationUser : IdentityUser
    {

    }

    public sealed class AccountContext : IdentityDbContext
    {
        private static bool _created = false;
        public DbSet Accounts { get; set; }
        public DbSet AccountMeta { get; set; }


        public AccountContext()
        {
            // Create the database and schema if it doesn't exist
            // This is a temporary workaround to create database until Entity Framework database migrations 
            // are supported in ASP.NET 5 
            if (!_created)
            {
                Database.Migrate();
                // this.Configuration.ProxyCreationEnabled = false;
                _created = true;
            }
        }
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer("Server=sql.alltours.ge;Database=TSS; User Id=sa; Password=Gitara123; MultipleActiveResultSets=true");
        }
        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);
            builder.Entity(e => {
                                             e.HasKey("Id");
                                             e.HasMany(a => a.AccountMeta).WithOne(a => a.Account); e.ToTable("Accounts");
            });
            builder.Entity(e => { e.HasKey("Id"); e.ToTable("Roles"); });

        }
    }

GVX111 avatar Oct 09 '15 12:10 GVX111

The Account class is being incorrectly mapped in the call to AddIdentity.

dcarr42 avatar Oct 10 '15 00:10 dcarr42

Its my account class

 public class Account : IdentityUser
    {
        public string Firstname { get; set; }
        public string Lastname { get; set; }
        public DateTime Birthdate { get; set; }
        public bool Gender { get; set; }
        public virtual ICollection AccountMeta { get; set; }
        public string GetGender()
        {
            return Gender ? "Male" : "Female";
        }
    }

Whats wrong?

GVX111 avatar Oct 10 '15 10:10 GVX111

Looks good I just saw this code and was not sure.

 public class ApplicationUser : IdentityUser
    {

    }

dcarr42 avatar Oct 10 '15 10:10 dcarr42

Diving into the codebase all of the identity classes use string keys by default, you are requesting an int key type:

https://github.com/aspnet/Identity/blob/5da757642dd7c7a25315250ae3738b87f622cb4c/src/Microsoft.AspNet.Identity.EntityFramework/IdentityEntityFrameworkServices.cs#L22-L23

dcarr42 avatar Oct 10 '15 11:10 dcarr42

Please derive from IdentityUser<int>. You can refer to this test: https://github.com/aspnet/Identity/blob/dev/test/Microsoft.AspNet.Identity.EntityFramework.Test/UserStoreIntKeyTest.cs#L9

rustd avatar Oct 14 '15 21:10 rustd