Reputation: 1001
I'm trying to add a Required property for ApplicationUser (ASP.NET Identity, MVC5)
Here's a simple example:
public class ApplicationUser : IdentityUser
{
[Required]
public string FirstName { get; set; }
}
The field is created in the database, but ends up NULLABLE
I expect the field to be NOT NULL.
Any idea how to get around this?
Upvotes: 4
Views: 6086
Reputation: 1295
This is more of a comment to @Number8 above (not allowed to comment at this time). I ran into same issue, which is probably relevant to the OP's issue. Database-Update checks the migration's Configuration class (Configuration.cs). If you have a seed method, you need to set default values for non-null properties in your seed method (or make them nullable in the model).
Upvotes: 0
Reputation: 21
I was able to make such a field not null by adding the following line to the seed method in the configuration.cs file
context.Database.ExecuteSqlCommand("ALTER TABLE [AspNetUsers] ALTER COLUMN [FirstName] nvarchar(MAX) NOT NULL");
Upvotes: 0
Reputation: 1001
Found a method to do this by combining the answers here and here
The final solution is really counterintuitive to me.
It seems that you need to explicitly have a separate table name for ApplicationUser.
Also, you must make sure that base.OnModelCreating(modelBuilder) is called first
If you don't do it this way, the field will never honor the NOT NULL aspect of the [Required] attribute.
Here is the code I used which finally got this working:
public class ApplicationUser : IdentityUser
{
public string FirstName { get; set; }
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
//modelBuilder.Entity<IdentityUser>().ToTable("Users"); // Won't work if the table name is the same.
modelBuilder.Entity<ApplicationUser>().ToTable("Users")
.Property(p => p.FirstName).IsRequired();
}
// etc.
}
Which results in the following DB structure:
Upvotes: 0
Reputation: 11568
Table per Hierarchy (TPH) mapping is the default in Entity Framework Code First. This means that if FirstName isn't required in all the classes in the type hierarchy that share the same table, then the column cannot be non-null.
If you want the FirstName column to be non-nullable you could choose a different mapping strategy. If you use Table per Type (TPT) instead, then you will have one table for the IdentityUser (AspNetUsers by default) and another for ApplicationUser. As the FirstName now is unique to the ApplicationUser table it can be non-nullable.
To use TPT you can override the OnModelCreating method like this:
protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<ApplicationUser>().ToTable("ApplicationUser");
}
Upvotes: 8
Reputation: 1021
I tried to add a non nullable string field in the IdentityUser using the Required attribute as well as overriding the OnModelCreating method as below:
protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
{
modelBuilder.Entity<ApplicationUser>().Property(x => x.FirstName).IsRequired();
base.OnModelCreating(modelBuilder);
}
No success on both. I don't know if this is by design, or a bug in the new ASP.NET Identity. I also searched for bug related but no success.
Maybe adding a migration would help you achieve what you need for now.
Upvotes: 0