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.
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
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With