I must be missing something trivial. I think this is the first time I'm encountering this.
I expanded my ApplicationUser : IdentityUser class with some basic data. Some of it is required, some is optional. A simplified extract:
public class ApplicationUser : IdentityUser
{
[Required]
public string FirstName { get; set; }
[Required]
public string LastName { get; set; }
[Column(TypeName = "Date")]
public DateTime? BirthDate { get; set; }
public string NationalNumber { get; set; }
}
Note that FirstName and LastName are required, NationalNumber is not. BirthDate, being nullable, isn't either. This results in the following automatically generated code:
CreateTable(
"dbo.AspNetUsers",
c => new
{
Id = c.String(nullable: false, maxLength: 128),
UserName = c.String(),
PasswordHash = c.String(),
SecurityStamp = c.String(),
FirstName = c.String(),
LastName = c.String(),
BirthDate = c.DateTime(storeType: "date"),
NationalNumber = c.String(),
Discriminator = c.String(nullable: false, maxLength: 128),
})
.PrimaryKey(t => t.Id);
The only columns which are created as nullable: false are the Id and Discriminator columns which are inherited from the IdentityUser class. Why are my [Required] attributes getting ignored ?
You are using inheritance and EF implements that using table per hierarchy by default. IdentityUsers and ApplicationUsers will be stored in the same table. But IdentityUsers don't have FirstName or LastName properties so the fields in the database have to be nullable. If that is a problem for you then you need to use one of the other strategies:
Table per type or table per concrete type
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