【问题标题】:Add View - Cannot retrieve Metadata添加视图 - 无法检索元数据
【发布时间】:2014-05-21 13:36:01
【问题描述】:

我仍然是 MVC 的新手,但我开始对总体情况有所了解,但我仍然混淆了 namespaceusing 之类的东西,我认为这可能是我误会的情况引用一些东西。

问题:我正在尝试添加一个 EmployeeInfo 视图,带有 List 模板,模型类:EmployeeInfo,数据上下文类:MoviesEntities

自动生成不会执行。当我在 Controller 的方法 EmployeeInfo 中单击鼠标右键时。我选择“添加视图”选项,填写信息,然后点击添加,在脚手架加载屏幕期间它给了我如下错误。

运行所选代码生成器时出错:'无法 检索“WebApplication2.Models.EmployeeInfo”的元数据。”

我的控制器

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using WebApplication2.Entities;
using WebApplication2.Models;

namespace WebApplication2.Controllers
{
    public class MoviesController : Controller
    {
        private MoviesEntities db = new MoviesEntities();

        // GET: /Movies/
        public ActionResult Index()
        {
            return View(db.Movies.ToList());
        }

        public ActionResult EmployeeInfo()
        {  
            var query =
            from m in db.Movies
            join me in db.MovieEmployees 
            on m.ID equals me.movieID
            join e in db.Employees
            on me.employeeID equals e.ID
            join r in db.Roles
            on me.roleID equals r.ID

            select new EmployeeInfo() {Name = e.Name, Role = r.RoleType, Birthday = e.Birthday };

            return View(query.Distinct().ToList());
          }
    }
}

我的背景

//------------------------------------------------------------------------------
// <auto-generated> Built from database Movie </auto-generated>
//------------------------------------------------------------------------------

namespace WebApplication2.Entities
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Infrastructure;
    using movieLayer;
    using WebApplication2.Models;

    public partial class MoviesEntities : DbContext
    {
        public MoviesEntities()
            : base("name=MoviesEntities")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }

        public virtual DbSet<Employee> Employees { get; set; }
        public virtual DbSet<Location> Locations { get; set; }
        public virtual DbSet<Movie> Movies { get; set; }
        public virtual DbSet<MovieEmployee> MovieEmployees { get; set; }
        public virtual DbSet<Role> Roles { get; set; }
        public virtual DbSet<Show> Shows { get; set; }
        public virtual DbSet<sysdiagram> sysdiagrams { get; set; }

        public System.Data.Entity.DbSet<WebApplication2.Models.EmployeeInfo> EmployeeInfoes { get; set; }
    }
}

EmployeeInfo的型号:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.Web.Mvc;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace WebApplication2.Models


{
    public class EmployeeInfo
    {

        public EmployeeInfo() { }

        public string Name { get; set; }
        public string Role { get; set; }
        public DateTime Birthday { get; set; }
    }
}

【问题讨论】:

  • 你的问题是什么?
  • 我正在尝试为 EmployeeInfo 创建一个视图
  • 错误在哪一行抛出?
  • @tereško 当我在 Controller 的方法 EmployeeInfo 中单击鼠标右键时,它不会运行自动生成。我选择选项,点击添加,在脚手架加载屏幕期间它给了我错误
  • @Andrei 没有行。在我为 EmployeeInfo 提交“添加视图”选项后,它停止并吐出错误。

标签: c# asp.net-mvc


【解决方案1】:

为了解决我在通过右键单击控制器操作添加脚手架视图时遇到的这个问题,我所做的是将“数据上下文类”字段留空。

我不完全确定为什么会这样,但我从 ASP.NET forums 找到了这个方法。

【讨论】:

  • @Toolkit 并非在所有情况下。如果您对我的回答投了反对票,我想知道原因,因为该答案可能对其他人有用。无论如何,感谢您的评论:)
  • 在我的情况下,这是在为 ViewModel 创建视图时发生的,在这种情况下,不需要指定数据上下文类。
【解决方案2】:

问题在于EmployeeInformation 只是控制器从视图查询中转储其数据的“模板”。

EmployeeInformation 实际上不是现有表,因此我必须创建一个模型,即EmpleeInfoModels 来保存数据,然后将其传递给我的自定义视图。

方法

public ActionResult EmployeeInformation()
        {
            var query =
            from m in db.Movies

            join me in db.MovieEmployees
            on m.ID equals me.movieID

            join e in db.Employees
            on me.employeeID equals e.ID

            join r in db.Roles
            on me.roleID equals r.ID

            select new EmployeeInfoModels() { Name = e.Name, RoleType = r.RoleType, Birthdate = e.Birthday, eID = e.ID };

            return View(query.Distinct().ToList().Distinct());
        }

型号

namespace WebApplication2.Models
{
    public class EmployeeInfoModels
    {
        public int mID { get; set; }
        public int eID { get; set; }
        public string Name { get; set; }
        public string RoleType { get; set; }
        public DateTime Birthdate { get; set; }
    }
}

我的视图刚刚引入

@model IEnumerable<WebApplication2.Models.EmployeeInfoModels>

【讨论】:

    【解决方案3】:

    确保首先重建您的解决方案。如果您刚刚添加了一个新类,那么如果项目尚未编译,脚手架引擎可能实际上并不知道它。

    如果这不起作用,请关闭 Visual Studio 并重新启动。我已经看到脚手架的东西在 VS2012 中无缘无故地出现问题,通常重新启动会修复它。

    【讨论】:

    • 我尝试重新启动,但没有成功。此外,我似乎也找不到“重建”选项。在我们的 Visual Studio 2012 上,它没有 MVC 5,所以我在这个练习项目中使用 Web Express 2013。
    • FWIW,您可以在 VS2012 中使用 MVC 5,您只需安装更新的 Web 工具:microsoft.com/en-us/download/details.aspx?id=41532。至于 2013 年,Build 菜单仍然在原来的位置,所以我不确定那里有什么让您感到困惑的地方。
    • 哦,我可以建造,只是不能重建哈哈。无论哪种方式,问题仍然存在:(
    【解决方案4】:

    在添加视图弹出窗口中检查您的DataContextClass 选项,然后在从控制器添加视图时更正ConnectionString

    Refer Screenshot .

    【讨论】:

      【解决方案5】:

      再添加一个提示:如果您的 web.config 中的任何元素中有任何 configSource 属性,您可能会遇到此错误。我注释掉了所有包含 configSource 属性的元素,然后再次运行脚手架,它就像一个魅力。完成后,我取消了 web.config 中元素的注释,一切就绪。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-03-04
        • 2013-11-16
        • 1970-01-01
        • 2019-07-19
        • 1970-01-01
        • 2015-12-17
        相关资源
        最近更新 更多