Database-first Add Controller not getting current model list - asp.net-mvc

I've recently dropped and added a few tables from my database-first generated model (asp.net mvc4).
Now, when i try to create a controller (via context menu add controller), and try to choose the new "Client" model from the list of available models, the dropdown list is out of sync, showing some old models that had been dropped still, but not showing any of the newly added models. (screenshot below)
What can I do to get this list of models updated so that I can continue? I've tried restarting VS, but the problem persists.
Thanks!

You have to rebuild the project for any new classes to show up.

Related

Updating scaffolded controllers and views with model changes

For the basic functionality of my project I've been defining Entity Framework models and then using the scaffolding feature of Visual Studio to implement CRUD functionality (Right Click -> Add -> New Scaffolded Item...).
If I need to make a change to the model after I've done this, how do I make the scaffolded controllers and views update to the latest model changes? For example, I'm seeing the need now to add a new attribute to my model and change the data type of an existing one, but the controllers and views likely won't work after the change.
I could always just re-scaffold, but I've made some changes to the controller and views already which I don't want to lose. Is my only solution to make the changes myself?
Rename the files having your changes, scaffold again, and replace the newly-scaffolded files with your originals.
Naturally, you'll have to reconcile any domain changes that were made to the files containing your changes.

An issue created due to experimentally selecting wrong scaffolding option in EF5 , MVC4 application

I am using VS2012 and EF5 first time. I developed application just like MVC3 , EF4.1 application without using any new feature. I already have a database , i created db context
, models , controllers and views. Every thing was working fine. Then i created a new table Directorate in database , created its model and database mapping in dbcontext.
While creating its controller i selected "MVC controller with read/write actions using Entity Framework" just as an experience to see what will happen. I selected my context
and model. When i run the app it created a new table in db with the name DirectorateModel and also inserted mapping in context. As i already have created table and mapping so i deleted each new functionalities added due to this action i.e. i deleted table, context mapping , controller and views.
but now when i run the application then it gives error:
The model backing the 'ArchievingDBContext' context has changed
since the database was created. Consider using Code First Migrations
to update the database
(http://go.microsoft.com/fwlink/?LinkId=238269)
I visited the mentioned url but it is about code first migrations that i don't want to use. Anyone please help me how i can fix this issue, i think i hagve to delete something
that was created due to selection of wrong scafolding option.
I just want my application to run in previously working state.
you just need to set correct database initilizer:
Database.SetInitializer<YourContextType>(new CreateDatabaseIfNotExists())
and this work
You can fix it by calling:
Database.SetInitializer(null);
in the Application_Start method of Global.asax
You don't want to be initializing on top of an existing database like that.
See: This Question
Remove the Directorate entity from your .EDMX model and the controller/views you've created using scaffolding on it.
Change your database as you want and then go to the VS Model designer (just open your .EDMX model) and Update model from database and save changes.
Now you can add views/controllers by scaffolding without any concern...

What are pros and cons of adding a Model to MVC using EF VS project before and after a Controller and a View?

In tutorials and walkthroughs on developing an MVC3 (MVC2) using EF (EntityFramework, Entity Framework 4.1/4.2) I observe quite different orders of adding Model, View, Controller to a project in a Microsoft Visual Studio 2010.
What are cons and pros of different orders of adding M, V and Cs?
and of, for example, adding a model before and, more specifically, after a view and controller?
There is no specific rules for adding one over the other first. When you create an empty ASP.NET MVC3 Project, It will come with some default folder structure which includes one Controller folder, Views folder and Models folder.
Now If you are beginner, This is what i suggest. Add a controller first.
Simply right click ont he Controller folder and Select Add->Controller from the context menu and add your first controller(Give the name as HomeController). It will come with a Default Index action method and you can see a return View statement. Run your project now. It will show you an error saying that it can not find a view. So now it is the time to add a view. Go the index action in home controller. Right click on the Return View() statement and select Add View that will add a view (index.xshtml) under the home folder under Views. Now run the app and you will see the page content.
If you are going to interact with your database, you can add model classes. If you can add POCO class file to your Models folder or you can have it on a different library which is refered to this projcet. It's all up to you.
As Lavinski mentioned, If you create your models first, You can use Scaffolding to create the controller actions for you. But If you are beginner, I would suggest you to create your controllers and views by hand. That will help you to understand MVC configuration works

MVC 3 and strongly-typed views

I have been having multiple problems with getting the Add View.. “Create a strongly-typed view” dialog box option to work properly.
Most of the time, I am unable to get the Model classes to show up in the “Model Class” drop down. This last issue is with Entity Framework classes that have been generated within the Model folder (and namespace). I Rebuild the project and they still fail to show up.
In fact, if I add a test class to the Model folder with a few public properties—it is not showing up in the drop down.
Is their any information available on how this drop down pulls the Model classes to display to the end-user?
Regards,
Normally rebuilding the project should be enough to show your custom class. But unfortunately this is far from perfect. Happens to me also. What I do is that I create my view model (not entity framework model because you should be passing only view models to your views), copy the name into the clipboard and paste it in the Add View dialog. I find this faster than scrolling through a dropdown of 1000 classes and finding the correct one.

After updating to MVC3 RC, why don't my View Data classes show up in the Add View dialog

Background:
I just updated to the RC of MVC3, and when I went to add a Stongly-Typed View, the list of classes didn't contain the class I wanted, which has been built in my project.
Now, I did compile my project, and already have several views that work correct, but their model also doesn't show up in this list.
Since the change to this dialog in RC, what is the new criteria that makes a class show up, considering none of them in my project showed up.
They altered the way that they're displayed in MVC 3 RC. For example previously you would just scroll to:
Project.Data.Models.User
Now it's organized in reverse
User (Project.Data.Models)
This is most likely your problem. What would be nice is if they had a filter to show only local objects rather than all referenced assemblies
Had the same problem. Clean solution and build and my view models started showing up...

Resources