-1

I want to add custom fields to AspNetCore.Identity and have done this

   public class ApplicationUser : IdentityUser
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }

I tried to do this

   public class ApplicationUser : IdentityUser
    {
        [Required]
        public string FirstName { get; set; }
        [Required]
        public string LastName { get; set; }
    }

but this does nothing.

chobo2
  • 83,322
  • 195
  • 530
  • 832
  • Did you add these to the DB table as well? – st_stefanov May 16 '18 at 17:54
  • What do you mean add to the DB table as well? I am letting EF generate the db, which it does and creates the fields but as "null" though, maybe it is the cmd I am running? I did "dotnet ef database update" – chobo2 May 16 '18 at 18:00
  • Ok, you use code first and the fields have appeared in the DB (are created). What is the issue that you have and what result are you trying to achieve? It's not clear what is the issue. – st_stefanov May 16 '18 at 18:05
  • Yes the database is being created with the fields, and I added FirstName and LastName which I do see, They are both marked as "null"when I want them "not null". I tried to add the annotation on them and remade the db, yet still get "not null" – chobo2 May 16 '18 at 18:09
  • You probably had other rows in the table beforehand. The DB won't allow you to include Not Null, unless the table is empty, because there is no default value for these columns – st_stefanov May 16 '18 at 18:10

1 Answers1

0

Generally, if a DB table is not empty, you can't add new columns which are "Not Null".

Now you can empty the DB and recreate these fields, or alter them manually to Not Null and you will be fine. Of course if there are records and you don't want to loose them, you will need to fill some value in the columns First and Last name

st_stefanov
  • 1,147
  • 1
  • 10
  • 28
  • I deleted the whole database and ran the cmd again. Though maybe something else is wrong? as I changed the column name on the second time around and it had the old name still. – chobo2 May 16 '18 at 18:20
  • Hm, that should have fixed the issue (I mean, drop the DB and call update-database from the EF command). Anyway, here is something similar: https://stackoverflow.com/questions/19566258/nullable-fields-are-created-when-customizing-identityuser-class-in-asp-net-ident – st_stefanov May 16 '18 at 18:27
  • I been following this: gttps://www.codeproject.com/Articles/1216796/ASP-NET-Core-Identity-Setting-up-a-web-project-and-the and they say to run: "dotnet ef migrations add Initial" then "dotnet ef database update". If I delete the intial file then I get the correct changes, though should it not be making a migration script each time a change happens? As when I add even new properties and run update I see: No migrations were applied. The database is already up to date. No migrations were applied. The database is already up to date. Done. – chobo2 May 16 '18 at 19:00
  • I myself moved to using Database First approach for the last 2 years, to have more control over the DB. I hope someone with more Code First experience could help here. Good luck. – st_stefanov May 16 '18 at 20:43