0

I'm having lots of trouble trying to figure out how to rebuild my database. I've deleted a table, then i ran this command update-Database -Verbose and it doesn't rebuild my database.

The same thing goes for when i have a table, then i change a column name inside my Model (C#) and then i want to rebuild my database so that the name shows up in the database, and nothing happens when i run the same command.

How can i rebuild my database? I'm sure there's a command or something, besides the update-Database -Verbose.

I'm using Visual Studio express 2012 for Web.

EDIT: Couldn't find a command able to rebuild my Database, though i did find a simple way to do what i wanted. You can delete tables or rename columns, in your Models, and it will always rebuild your database. In the Solution explorer, you'll find an icon that says "Show All Files", press it and open the folder App_Data, there you'll see your database file, delete it and re-run your application, and it will build again the Database, with all the changes you've made to your Models in your code. So that part about changing catalog and .mdf names in the Web.config file is not needed.

1 Answer 1

1

Update-Database is part of the Entity Framework Migrations package, which allows you to script the changes to the database sequentially. The command won't do anything on it's own without migrations to process. If you are using Migrations, you need to use the Add-Migration myNewMigration command first, and verify the script that is generated makes sense.

If you are not using Migrations, you can use the much simpler database options such as DropCreateDatabaseIfModelChanges() against your context class.

Another thing you can try is running Update-Database -f to "force" the migration to run even if it's already present in the _MigrationHistory

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

11 Comments

I'm using Migrations. Just added a new migration with Add-Migration m1 and the file didn't have anything inside, just a method up() and down() with nothing inside. Then i did update-Database -Verbose again and nothing happens to the database in the Database explorer. I don't know if it's normal but when i run my website, through "Start without debugging" it shows the information i had in the table i deleted, so what's up with that? I'm currently learning ASP.NET MVC 4, so this is all just new for me.
Actually, the information it's showing, when i run the website, is from a table that i have not deleted, so in that matter, things are ok, but i still cant rebuild my database, no matter what.
up() and down() with nothing inside means that there weren't any changes to your model that would require a change to your database. Can we clarify a few things here... when you say you deleted a table, are you meaning that you removed it in the Database Explorer and are expecting it to be put back by your application?
Yes, i removed it from my Database Explorer, because i thought it would be "built" again, like the application built the database the first time i ran it. So yes, i'm expecting for it to be put back by my application. I already tried a initializer in the global.aspx, where i inserted: Database.SetInitializer(new DropCreateDatabaseAlways<MyDbContext>()); And still nothing changed in my DB in the database explorer, when i build my app.
the Initializer would take effect when you Run the application; the code isn't processed just by the build. As to your issue with the migrations, there is a special table created for migrations, the _MigrationHistory table. Migrations are run sequentially by their date, and the run is added to the history table. even if the database is changed, a specific migration won't be run again if it exists in the _MigrationHistory.
|

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.