3

I am trying to add a few extra fields to the register page in the default asp.net mvc application that we get when we open visual studio. I followed this tutorial.

Customize MVC 5 Register Page

These are the steps that I followed after opening the default project that we get in Visual studio for asp.net MVC.

  1. First I ran the project and clicked on Register button and registered a random user. I did this so that the default database would get created.

    enter image description here

    1. Now that the database was generated, I went to the database and added extra columns like Gender, Birthday, FirstName, LastName and AboutUser and re-updated the database table called ASPNETUSERS.

    enter image description here

    1. Now I went to RegisterViewModel and added these same properties as well.

    enter image description here

    1. After that I went to ApplicationUser and added the same properties as well.

    enter image description here

    1. After that I changed the view for Register.cshtml to incorporate extra textboxes, textareas for the new fields that I created.

    2. Now I went to the controller and added these properties in as well. Everything seems fine to me so far, except the fact that the database's definition changed from the beginning.

    enter image description here

This is how the page looks after all these changes.

enter image description here

Now I ran the project and tried to register a person, but I keep getting this error.

enter image description here

I researched about this error, but the solutions suggest that I should try adding Database.SetInitializer<YourDbContext>(null); in the constructor of myDBContext. But the thing is, I don't have any context at all. So this solution doesn't seem to work for me.

7
  • 1
    You shouldn't modify the DB directly. I'd suggest you delete the fields you added and follow @Latvian's answer Commented Jul 28, 2016 at 22:11
  • So I have to do that thing first and then change my database structure? or can I do that now to fix this? Commented Jul 28, 2016 at 22:20
  • 1
    No, migrations are how you're supposed to change the database schema. Add the properties to ApplicationUser as you have, then run the migration steps. You might have to run enable-migrations first. You'll want to read up on Code First. Commented Jul 28, 2016 at 22:23
  • 1
    You shouldn't touch the database at all. Use the migrations Commented Jul 28, 2016 at 22:57
  • 1
    -IgnoreChanges just tells the migrations to skip the schema check. In your next project do enable-migrations first and you won't have that issue. Commented Jul 28, 2016 at 23:15

3 Answers 3

4

When you change any entity in solution, you should change your db (so your object model equals your db model).

In this case you should open nuget package console (service > nuget > console) and use:

       add-migration <YourNewMigrationName>

and after new migration created (code for change your db model), you have to accept it:

       update-database
Sign up to request clarification or add additional context in comments.

Comments

0

just remove the record added to migration table

Comments

0

Delete your Migrations Folder And Reapply migrations

2 Comments

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.