Add Controller Model Classes not shown

自古美人都是妖i 提交于 2019-11-29 13:24:44

问题


I managed to create a Model First DBContext model (before it was a normal ObjectContext derived model).

Strangly now my VS is not showing ANY of my classes in the Model selectlist in "Add Controller". More or less everything external is shown here - except my own stuf. Yes - i compiled the code, the code is compiling without errors and restarted VS multiple times. No clue what the heck is going on.

Same in the Data Context Class selector.

Manuelly setting the class is not working. Add will always complain I have to choose from the list.

Where is VS getting the data for filling the list?

Framework: MVC4, VS2010, EF4.3.1


回答1:


Had the same problem. Found that just Building the project didnt solve the problem of the missing Model classes. Rebuilding the project however, solved it right away.




回答2:


The reason it worked is that by putting it in another project you "built" it and then it showed up. If you would have built your first project and then tried to add a controller bam it would be there.




回答3:


"Add Controller" Model Classes not shown for scaffolding

Environment Used: ASP.NET MVC5 using C# Visual Studio 2013

Solution 1: If solution name is SAME as project name, just clean solution, Build Solution. It will work.

Solution 2: If solution name is DIFFERENT from project name, then check below steps:

(i) Check your model class(s) namespace as

namespace SolutionName.ProjectName.Models
{ ...}

suppose solution name is "WGT" and project name is "MS", then models class say User should be

namespace WGT.MS.Models
{ public class User{...}}

(ii) After this, clear solution.

(iii) Build Solution

It will surely work. Found this solution after spending a lot of time. It's a practical and implemented solution.




回答4:


Had the same problem. I fixed it.

  • Separate the EF in another class library project.
  • add reference in main web project.
  • add the connection string in web project config file.



回答5:


Finally I managed to get the scaffolding working again. I refactored my model.edmx out of the main project. The templating stuff will now generate the model classes in this new model project. After adding the model project to the main project, the classes are showing up in the controller model selectbox again. Strange as it seems to work in a completely new set-up solution without separation...




回答6:


Possibly related: Add View Model class dropdown not showing my class

I was looking for a solution to this when adding a controller, but reached the mentioned post earlier than reaching this one. Removing AssemblyInfo.cs and setting the properties on the project worked for me (for VS2010).




回答7:


Had the same problem with an EF model in a separate project to begin with.

Rebuild all did nothing, but simply reloading into a new instance of Visual Studio fixed it.




回答8:


I got the same issue , Check the package (asp.net mvc, & EF ) compatibility , upgrade all nuget packages. clean and Rebuild project. That works for me.

http://forums.asp.net/t/1791616.aspx?Can+t+create+new+controller+No+model+classes+are+available+




回答9:


Create a new MVC internet site. Add existing (your model classes) - verify it works, then start diffing the two projects.




回答10:


I just experienced this issue in a project that had been working. There were NO updates to EF or MVC, it just started throwing the "There no model classes..." message. I backed up my project, deleted it from my HD and the got the most recent version from source control, SVN in this case. I rebuilt the project and now it works again, just like magic.

VS 2012 EF 5.0 .NET 4.5 MVC 4.0




回答11:


Building / rebuilding did not work for me. I had to close and reopen the solution, and then the class finally appeared.




回答12:


I had this same issue with one specific model. I was using .Net Core 2.x with EF Core. Turned out it was because it was inheriting a class which was, in turn, inheriting Microsoft.AspNetCore.Identity.IdentityUser. EF Core will not let me scaffold from that model. I removed the inheritance of IdentityUser from the base class and the model appeared in the dropdown for EF new Controller.




回答13:


This problem can occur when you have a project referencing another project. This is called a transitive project reference and manifests in a number of ways. You need to directly reference projects and not transitively to resolve this in some cases.

eg: Project A , project B, Project C Project B has a reference to C so in repos/b/bin/debug you have b.dll and c.dll Project A needs both so you point the reference to B.dll and C.dll in the b/bin/debug folder - you can't see any models from c.dll but you can see the b.dll models

fix/workaround: reference repos/b/bin/debug/b.dll and repos/c/bin/debug/c.dll directly




回答14:


I was having a similar problem, despite ensuring that I included

using ProjectName.Models; 

at the top of my controller file. Building/Rebuilding didn't work for me. Nor did closing all instances of Visual Studio, and reopening project.

I thought the problem might be that we are using TFS (Team Foundation Server by Microsoft). I had the problem because I initially created the model and then created my controller within the views folder, before I moved that controller (file) to the controllers folder.

My solution was to create my model, copy controller code elsewhere, delete the controller, rebuild/build the solution. Then I created a new controller, copied the correct code back in, and hey presto, it recognised the model class.



来源:https://stackoverflow.com/questions/9758493/add-controller-model-classes-not-shown

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!