Question

When I create a model for a table, but no view or controller, then run Add-Migration, no migration code is generated for the new model. However, if I create a view and controller for the table using Visual Studio's built-in Add Controller wizard, then migration code is generated. But I also noticed that if I generate the view and controller with the wizard and then delete the view and controller, migration code is still generated.

That must mean that Visual Studio is doing something behind the scenes when I generate the view and controller with the wizard that triggers migration code creation. What is this something?

Was it helpful?

Solution

The code migration triggers when you add your fresh model (table) into your DbContext class. After that any change you make to your model class will trigger migration aswell. I believe wizard does this for you. When you select your Model and DbContext class from the wizard, it automatically adds new DbSet<ModelType> property into your DbContext class. When you delete your controller and views the DbSet is not deleted from the context class.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top