生成 ScaffoldingConnectionFactory 时出错

发布于 2024-11-02 21:16:04 字数 1297 浏览 1 评论 0原文

我有一个名为 Applicant 的简单课程。我正在尝试使用实体框架添加一个模板控制器,并将 Applicant 作为我的模型类,以及一个新的数据上下文。

每次我尝试创建控制器时,都会收到一个错误对话框,其中显示“无法检索“MyNameSpace.Models.Applicant”的元数据。生成“ScaffoldingConnectionFactory”时出错。尝试重建项目。”

重建没有任何作用。

这是我的模型类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;

namespace MyNameSpace.Models
{
    public class Applicant
    {
        public int ApplicantId { get; set; }
        [Required]
        public string FirstName { get; set; }
        [Required]
        public string LastName { get; set; }
        public string MiddleInitial { get; set; }
        [Required]
        public string Phone { get; set; }
        [Required]
        public string EmailAddress { get; set; }
        [Required]
        [DataType(DataType.Date)]
        public string DateOfBirth { get; set; }
        public virtual Ethnicity Ethnicity { get; set; }
        public virtual Race Race { get; set; }
    }

    public class Ethnicity 
    {
        public int EthnicityId { get; set; }
        public string Type { get; set; }
    }
    public class Race 
    {
        public int RaceId { get; set; }
        public string Type { get; set; }
    }
}

我觉得我错过了一步,但我无法指出它。

I have a simple class called Applicant. I'm trying to add a template controller using the Entity Framework with Applicant as my model class, and a new data context.

Every time I try to create the controller, I get an error dialog that says "Unable to retrieve metadata for 'MyNameSpace.Models.Applicant'. There was an error generating 'ScaffoldingConnectionFactory'. Try rebuilding your project."

Rebuilding does nothing.

Here is my model class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;

namespace MyNameSpace.Models
{
    public class Applicant
    {
        public int ApplicantId { get; set; }
        [Required]
        public string FirstName { get; set; }
        [Required]
        public string LastName { get; set; }
        public string MiddleInitial { get; set; }
        [Required]
        public string Phone { get; set; }
        [Required]
        public string EmailAddress { get; set; }
        [Required]
        [DataType(DataType.Date)]
        public string DateOfBirth { get; set; }
        public virtual Ethnicity Ethnicity { get; set; }
        public virtual Race Race { get; set; }
    }

    public class Ethnicity 
    {
        public int EthnicityId { get; set; }
        public string Type { get; set; }
    }
    public class Race 
    {
        public int RaceId { get; set; }
        public string Type { get; set; }
    }
}

I feel like I've missed a step but I can't put my finger on it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

凉城 2024-11-09 21:16:04

我遇到了同样的问题,并且必须以类似的方式解决它。

我的 EF 版本与新的 MVC 工具不兼容。从 NuGet 更新 EntityFramework 并对过时的代码进行一些更新后,一切正常。

I had the same problem and had to fix it similarly.

My version of EF was incompatible with the new MVC tooling. After updating EntityFramework from NuGet, and making a few updates to outdated code, everything is working fine.

这是一个糟糕的答案,我想了解根本原因,但我的解决方案是从 NuGet 卸载 EntityFramework 库包引用,然后重新安装它。现在一切正常了。

This is a terrible answer and I'd like to understand the root cause, but my solution was to uninstall the EntityFramework library package reference from NuGet, then re-install it. Everything works now.

多彩岁月 2024-11-09 21:16:04

我有同样的问题。尝试使用 NuGet 重新安装 EntityFramework。

没有修复任何东西。

我准备放弃并尝试使用 Linq。

我以为我早些时候就可以工作了!

http://www.asp.net /mvc/tutorials/creating-model-classes-with-linq-to-sql-cs

有时,MS 堆栈似乎总是变化太大,以至于不可靠。这就是人们使用 Linq 的原因吗?

编辑:在 MS Visual Web Designer 上运行 Web 平台安装程序修复了此问题。

I had the same issue. Tried re-installing EntityFramework using NuGet.

Didn't fix anything.

I'm about to bail and try using Linq.

I thought I had this working earlier!!

http://www.asp.net/mvc/tutorials/creating-model-classes-with-linq-to-sql-cs

Sometimes it seems like the MS stack is always changing so much it's unreliable. Is that why people use Linq?

EDIT: Running web platform installer on MS Visual web designer fixed this problem.

回心转意 2024-11-09 21:16:04

我刚刚遇到这个问题,然后我意识到我必须选择正确的 NuGet 包来避免这个问题。 NuGet包列表中有两个非常熟悉的包。

  1. EFCodeFirst.SqlServerCompact 与实体框架功能 CTP5 一起使用。
  2. EntityFramework.SqlServerCompact 与 Entity Framework 4.1 一起使用。

如果您使用的是 Entity Framework 4.1,则应使用“EntityFramework.SqlServerCompact”!

I was just met this problem and then I realize that I have to choose the right one NuGet package to avoid this problem. There are two very familiar packages in the NuGet package list.

  1. The EFCodeFirst.SqlServerCompact is used with Entity Framework Feature CTP5.
  2. The EntityFramework.SqlServerCompact is used with the Entity Framework 4.1.

The "EntityFramework.SqlServerCompact" should be used if you are using Entity Framework 4.1 !!

荒岛晴空 2024-11-09 21:16:04

当我尝试使用“添加控制器”对话框创建控制器并选择“具有读/写操作和视图的控制器,使用实体框架”模板选择时,我遇到了这个问题。

这会生成 Controller 类的代码。顺便说一句,这包括对 EntityFramework Context 对象的引用(例如,在我的 PersonController 示例中,以下行:“Person person = db.People.Find(id);”)。这仅作为起点,当然,如果您愿意,您可以替换自动生成的代码。不管怎样,这就是为什么它在对话框中要求一个 Context 类,以及为什么它会根据要求自动生成一个新的类。

我使用Nuget引用Entity Framework,它具有引用EntityFramework.dll v4.0的效果。后来我将数据访问代码抽象到另一个项目中,然后通过 Nuget 卸载了 EF。存在一些对 DbSet<> 的现有引用。在 Context 类中,我想我必须通过让 Resharper 添加引用来获取项目以使其编译/构建来解决它。 R# 添加了对 .Net 4 Framework 库中的 System.Data.Entity 的引用。正是在这个阶段,当我尝试使用“具有读/写操作和视图的控制器,使用实体框架”模板创建新控制器时,我开始收到错误。

我通过以下方法解决了这个问题:(1) 删除对 System.Data.Entity 的所有引用; (2)通过Nuget安装Entity Framework并重建; (3) 创建控制器; (4) 通过 Nuget 卸载 EF,删除自动生成的 Context 类,并将控制器中自动生成的代码替换为我自己的代码,该代码引用了我的 DAL 项目/程序集中的类。

I got this problem when I tried to create a controller using the "Add Controller" dialog box, and by selecting the "Controller with read/write actions and views, using Entity framework" Template selection.

This generates code for a Controller class. By the way, this includes references to an EntityFramework Context object (such as, in my case for a PersonController, the following line: "Person person = db.People.Find(id);"). This is for a starting point only, and of course you can replace the auto-generated code, if you wish. Either way, this is why it asks for a Context class in the dialog, and why it will auto-gen a new one for you if requested.

I referenced Entity Framework using Nuget, which had the effect of referencing EntityFramework.dll v4.0. I later abstracted out the Data Access code into a different project, so then uninstalled EF through Nuget. There were some existing references to DbSet<> in the Context class, and I think I must have resolved it by getting Resharper to add a reference to get the project simply to get it to compile/build. R# added a reference to System.Data.Entity from the .Net 4 Framework library. It was at this stage that I started getting the error when trying to create new Controllers using the "Controller with read/write actions and views, using Entity framework" template.

I resolved it by (1) removing all references to System.Data.Entity; (2) installing Entity Framework through Nuget and rebuilding; (3) Creating the controller(s); (4) uninstalling EF through Nuget, removing the auto-generated Context class and replacing the autogenerated code in the controller with my own that referenced my classes from my DAL project/assembly.

戴着白色围巾的女孩 2024-11-09 21:16:04

有同样的问题。问题是上下文位于单独的程序集中。

    namespace WebJhs179.Models {

        /// <summary>
        /// This class is just wrapper to actual context. Because actual context is in separate assembly, 
        /// MVC3's controller creation fails. 
        /// Workaround: Create empty class and inherit actual context.
        /// </summary>
        public class Jhs179Context : Jhs179.Jhs179DbContext {
        }
    }

Had same problem. Problem is that the context is in separate assembly.

    namespace WebJhs179.Models {

        /// <summary>
        /// This class is just wrapper to actual context. Because actual context is in separate assembly, 
        /// MVC3's controller creation fails. 
        /// Workaround: Create empty class and inherit actual context.
        /// </summary>
        public class Jhs179Context : Jhs179.Jhs179DbContext {
        }
    }
断肠人 2024-11-09 21:16:04

我在 ASP.NET MVC 4 中添加新控制器时遇到了同样的问题,我通过将 Database.SetInitializer(new SomeDataContextInitializer()); 从 DBContext 的构造函数移动到 Application_Start 方法解决了这个问题Global.asax.cs。
希望有帮助。

I had the same problem while adding new controller in ASP.NET MVC 4, I solved the problem by moving the Database.SetInitializer(new SomeDataContextInitializer()); from the constructor of the DBContext to Application_Start method in Global.asax.cs.
Hope it helps.

去了角落 2024-11-09 21:16:04

这些工具是否期望您的实体框架 EDMX 与您的网站位于同一程序集中?
我在 EDMX 位于单独程序集中的项目中发生了相同的错误。
这是必要的,因为我们需要网站和服务的数据库访问。

Can it be that the tools expect your Entity Framework EDMX to be in the same assembly as your website?
I've had the same error occuring in a project where the EDMX was in a separate assembly.
This was nessesary because we needed database access for a website as well as for a service.

紫罗兰の梦幻 2024-11-09 21:16:04

我在使用 EF 4.3.1 时遇到了同样的问题。就我而言,这是因为我使用部分类向生成的 DbContext 附加了额外的功能。当我从模型项目中删除此类时,脚手架可以正常工作。

I had the same problem using EF 4.3.1. In my case it was because I had attached extra functionality to the generated DbContext using a partial class. When I removed this class from the model project, the scaffolding worked correctly.

无需解释 2024-11-09 21:16:04

就我而言,.edmx 和模型类位于单独的程序集中。这不是问题,因为我只需删除用于扩展生成的 DbContext 类的部分类即可使脚手架正常工作。

In my case the .edmx and model classes were in a separate assembly. This was not the issue, as I was able to get the scaffolding to work correctly simply be removing a partial class that I had used to extend the generated DbContext class.

沉鱼一梦 2024-11-09 21:16:04

我试图使用MVC 4和Entity Framework 6。我终于弄清楚MVC4默认引用EF5 dll,卸载并安装EF6。他们我收到消息:

MVC 脚手架不支持实体框架 6 或更高版本。为了
更多信息请访问:
[http://go.microsoft.com/fwlink/?LinkId=276833][1]

解决方案是改用 MVC 5,因为我已经编写了 EF6 代码,并且 MVC 应用程序是新的。

I was trying to use MVC 4 and Entity Framework 6. I finally figured out that MVC4 references the EF5 dll by default, uninstalled and installed EF6. They I got the message:

MVC Scaffolding does not support Entity Framework 6 or later. For
more information please visit:
[http://go.microsoft.com/fwlink/?LinkId=276833][1]

Solution was to use MVC 5 instead, as I had already written EF6 code, and the MVC app was new.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文