asp.net-mvc 添加未显示的控制器模型类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9758493/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 01:48:30  来源:igfitidea点击:

Add Controller Model Classes not shown

asp.net-mvcvisual-studio-2010entity-framework-4scaffolding

提问by Obiwan007

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

我设法创建了一个 Model First DBContext 模型(在它是一个普通的 ObjectContext 派生模型之前)。

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.

奇怪的是,现在我的 VS 没有在“添加控制器”的模型选择列表中显示我的任何类。或多或少的所有外部内容都显示在这里 - 除了我自己的东西。是的 - 我编译了代码,代码编译没有错误并且多次重新启动 VS。不知道到底发生了什么。

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?

VS 在哪里获取填充列表的数据?

Framework: MVC4, VS2010, EF4.3.1

框架:MVC4、VS2010、EF4.3.1

回答by poeticGeek

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.

有同样的问题。发现只是Build the project并没有解决缺少Model类的问题。然而,重建项目,马上解决了。

回答by Joshua Schipper

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.

它起作用的原因是,通过将它放在另一个项目中,您“构建”了它,然后它就出现了。如果你已经构建了你的第一个项目,然后尝试添加一个控制器 bam 它就会在那里。

回答by Varinder Hatta

"Add Controller" Model Classes not shown for scaffolding

脚手架未显示“添加控制器”模型类

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

使用环境:ASP.NET MVC5 使用 C# Visual Studio 2013

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

解决方案 1:如果解决方案名称与项目名称相同,只需清理解决方案,构建解决方案。它会起作用。

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

解决方案 2:如果解决方案名称与项目名称不同,请检查以下步骤:

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

(i) 检查您的模型类命名空间为

namespace SolutionName.ProjectName.Models
{ ...}

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

假设解决方案名称是“WGT”,项目名称是“MS”,那么模型类说 User 应该是

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

(ii) After this, clear solution.

(ii) 在此之后,清除溶液。

(iii) Build Solution

(iii) 构建解决方案

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

它肯定会起作用。花了很多时间后找到了这个解决方案。这是一个切实可行的解决方案。

回答by atik sarker

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.
  • 在另一个类库项目中分离EF。
  • 在主 Web 项目中添加引用。
  • 在 web 项目配置文件中添加连接字符串。

回答by Parag Doke

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).

我在添加控制器时正在寻找解决方案,但在到达这个帖子之前到达了提到的帖子。删除 AssemblyInfo.cs 并设置项目的属性对我有用(对于 VS2010)。

回答by Gone Coding

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

开始时在单独的项目中使用 EF 模型时遇到了同样的问题。

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

Rebuild all 什么也没做,只是重新加载到 Visual Studio 的新实例中即可修复它。

回答by Obiwan007

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...

最后我设法让脚手架再次工作。我从主项目中重构了我的 model.edmx。模板内容现在将在这个新模型项目中生成模型类。将模型项目添加到主项目后,类再次显示在控制器模型选择框中。奇怪的是,它似乎可以在一个全新的设置解决方案中工作而无需分离......

回答by racsonp

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.

我遇到了同样的问题,检查包 (asp.net mvc, & EF) 兼容性,升级所有 nuget 包。清理并重建项目。这对我行得通。

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

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

回答by slasky

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.

我在一个特定模型上遇到了同样的问题。我使用 .Net Core 2.x 和 EF Core。原来这是因为它继承了一个类,而该类又继承了 Microsoft.AspNetCore.Identity.IdentityUser。EF Core 不会让我从该模型搭建脚手架。我从基类中删除了 IdentityUser 的继承,模型出现在 EF 新控制器的下拉列表中。

回答by Rodger

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, one of which is this. You need to directly reference projects and not transitively to resolve this in some cases.

当您有一个项目引用另一个项目时,可能会出现此问题。这被称为可传递的项目引用,并以多种方式体现,其中之一就是这样。在某些情况下,您需要直接引用项目而不是通过传递来解决此问题。

eg: Project A , project B, Project C

例如:A 项目、B 项目、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. If you do this you can't see any models from c.dll but you can see the b.dll models.

项目 B 有对 C 的引用,因此在 repos/b/bin/debug 中,您有 b.dll 和 c.dll 项目 A 需要两者,因此您将引用指向 b/bin/debug 文件夹中的 B.dll 和 C.dll . 如果您这样做,您将看不到 c.dll 中的任何模型,但您可以看到 b.dll 模型。

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

修复/解决方法:直接引用 repos/b/bin/debug/b.dll 和 repos/c/bin/debug/c.dll