How to create custom fields for IdentityUser in asp.net Core 2.1.1

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



How to create custom fields for IdentityUser in asp.net Core 2.1.1



I have created a Model which extends the "IdentityUser" class.


public class ApplicationUser : IdentityUser

public string FirstName_TXT get; set;
public string LastName_TXT get; set;



When defining the dbcontext I have made sure to include the IdentityDbContext as well as the ApplicationUser Reference.


//initial DB Configuration
public class DbContext : IdentityDbContext<ApplicationUser>

//Users References Articles
//Articles Automaticalled gets create
public DbSet<ContactRequest> ContactRequests get; set;
public DbSet<Class> Classes get; set;

public DbContext()




public DbContext(DbContextOptions options)
: base(options)




protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)

optionsBuilder.UseSqlServer(@"Server=.SQLEXPRESS;Database=DBNAME;Trusted_Connection=True;");


protected override void OnModelCreating(ModelBuilder modelBuilder)

base.OnModelCreating(modelBuilder);




When I attempt to create the DB with the additional fields in a console application, I get the following error.


class Program

static void Main(string args)


using (var ctx = new DbContext())

ctx.Database.EnsureCreatedAsync();

ctx.SaveChanges();






System.InvalidOperationException: 'A key cannot be configured on 'ApplicationUser' because it is a derived type. The key must be configured on the root type 'IdentityUser'. If you did not intend for 'IdentityUser' to be included in the model, ensure that it is not included in a DbSet property on your context, referenced in a configuration call to ModelBuilder, or referenced from a navigation property on a type that is included in the model.'



Not sure what I am missing. Any suggestions would be greatly appreciated.




1 Answer
1



The key property cannot be ignored by design. IdentityUser has a default key property Id.
Your ApplicationUser class doesn't specify the type of its Id field. I put my code


IdentityUser


public class ApplicationUser : IdentityUser<string>

public string FirstName_TXT get; set;
public string LastName_TXT get; set;



you can use integer type Id key property ,for this use IdentityUser<int>


IdentityUser<int>





When I attempted to make this change, it left me with another error when defining the DbContext "The type cannot be used as type parameter 'TUser' in the generic type or method 'IdentityDbContext<TUser>'. There is no implicit reference conversion from to 'ApplicationUser' to 'IdentityUser' " public class DbContext : IdentityDbContext<ApplicationUser>
– Logick
Aug 7 at 2:01



public class DbContext : IdentityDbContext<ApplicationUser>





you will need to have an ApplicationRole class similar to you ApplicationUser class. if use key int type then use this : public class DbContext :IdentityDbContext<ApplicationUser,ApplicationRole,int>
– Maksud
Aug 14 at 11:05







By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard