Ziyan Junaideen
Ziyan Junaideen

Reputation: 3300

ASP.NET MVC4 - Error When Customize Default Accounts Models & Controller

I am in the begining of making a simple website using ASP.NET MVC4 CodeFirst Approach. The web site is built around users who are able to register, make posts etc. The existing UserProfile class was modified for the accommodation other fields (ex: FirstName, LastName etc).

When I ran the website I got a similar error:

Invalid column name 'FirstName'.
Invalid column name 'LastName'.
Invalid column name 'Phone'.

I red that this is because the Database is not updated as the model is updated. So I set the following on the Application_Start() in Global.asax with the intention of droppnng the database always (at least until I get the hang of it).

protected void Application_Start()
    {
        Database.SetInitializer(new DropCreateDatabaseAlways<UsersContext>());
        //Other default generated stuff below...
    }

I also tryed the DropCreateDatabaseIfModelChanges, but both methods didn't drop the database.

Isn't this possible? Why is it? Am I doing some thing wrong?

I believe it would be possible to store the info in a different table and link it to this, but I prefer to keep it in one table.

Also is it a bad idea to add UserProfiles to the websites context (lets say: DatabaseContext (which has other entities like Posts, Comments etc) and change the AccountsController to use DatabaseContext instead of UsersContext?

Thanks in advance.

Upvotes: 0

Views: 293

Answers (1)

keneco
keneco

Reputation: 81

Have you run Enable-Migrations in the Package Manager Console? You should see a migration folder in your project, with a configuration.cs file, ensure you have enabled automatic migrations.

public Configuration(){
    AutomaticMigrationsEnabled = true;
    //if you drop columns - consider this carefully...
    AutomaticMigrationDataLossAllowed = true;
}

Upvotes: 1

Related Questions