ASP.Net ID for MVC 6 with INT id Column

Im using an MVC6 project with Asp.net id and wanted to change the id column from row to int. I followed this article enter the link here

I get an error message that can insert zero in the identifier columns for the role and user, but if I return to normal, it will work.

public class ApplicationUser : IdentityUser<int>
{
}
public class ApplicationRole : IdentityRole<int>
{
}

public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, int>
{
    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);
        // Customize the ASP.NET Identity model and override the defaults if needed.
        // For example, you can rename the ASP.NET Identity table names and more.
        // Add your customizations after calling base.OnModelCreating(builder);
    }

    public DbSet<PropertyManagementCompany> PMC { get; set; }
}
+4
source share
3 answers

I did it for MVC5 before what I actually did

#region Entities

public class ApplicationUserClaim : IdentityUserClaim<Int32> { }
public class ApplicationUserRole : IdentityUserRole<Int32> { }
public class ApplicationUserLogin : IdentityUserLogin<Int32> { }
public class ApplicationRole : IdentityRole<Int32, ApplicationUserRole> { }
public class ApplicationUser : IdentityUser<Int32, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>, IUser<Int32> { }
public class ApplicationClaimsPrincipal : ClaimsPrincipal
{
    public ApplicationClaimsPrincipal(ClaimsPrincipal claimsPrincipal) : base(claimsPrincipal) { }
    public Int32 UserId { get { return Int32.Parse(this.FindFirst(ClaimTypes.Sid).Value); } }
}

#endregion

#region Stores

public class ApplicationUserStore : UserStore<ApplicationUser, ApplicationRole, Int32, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{
    public ApplicationUserStore() : base(new CustomsSiteDbContext()) { }
    public ApplicationUserStore(CustomsSiteDbContext context) : base(context) { }
}
public class ApplicationRoleStore : RoleStore<ApplicationRole, Int32, ApplicationUserRole>
{
    public ApplicationRoleStore() : base(new CustomsSiteDbContext()) { }
    public ApplicationRoleStore(CustomsSiteDbContext context) : base(context) { }
}

#endregion

#region Managers

public class ApplicationUserManager : UserManager<ApplicationUser, Int32>
{
    public ApplicationUserManager() : base(new ApplicationUserStore()) { }
    public ApplicationUserManager(ApplicationUserStore userStore) : base(userStore) { }
}
public class ApplicationRoleManager : RoleManager<ApplicationRole, Int32>
{
    public ApplicationRoleManager() : base(new ApplicationRoleStore()) { }
    public ApplicationRoleManager(ApplicationRoleStore roleStore) : base(roleStore) { }
}

#endregion

I hope this helps

+1
source

Here's how to use an integer column in Identity, Asp NET Core, and Entity Framework Core:

public class User : IdentityUser<int>
{
}

public class Role : IdentityRole<int>
{
}

public class AppDbContext : IdentityDbContext<User, Role, int>
{
    public AppDbContext(DbContextOptions options) : base(options) {}
}

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddIdentity<User, Role>(options => {
            // ...
        }).AddEntityFrameworkStores<AppDbContext, int>(); // NOTE this line
    }
}

:

Unhandled Exception: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.ArgumentException: GenericArguments[0], '...Models.User', on 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore`3[TUser,TRole,TContext]' violates the constraint of type 'TUser'. ---> System.TypeLoadException: GenericArguments[0], '...Models.User', on 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore`4[TUser,TRole,TContext,TKey]' violates the constraint of type parameter 'TUser'.

, TKey AddEntityFrameworkStores<AppDbContext, int>().

+3

. . VS2015 , , INT . , ,

  • ASP.Net
  • Migrations
  • "dnx ef migrations add"
  • Identity, PK.
+1

Source: https://habr.com/ru/post/1613707/


All Articles