在浏览器中尝试搭建控制器后,未映射该类型

时间:2012-01-09 20:34:08

标签: asp.net asp.net-mvc-3 scaffolding

我收到错误:

  

未映射“GMS_Sandbox_MVC.Models.Organization”类型。校验   使用忽略未明确排除类型   方法或NotMappedAttribute数据注释。验证类型是否   定义为类,不是原始的,嵌套的或通用的,也不是   继承自EntityObject。   源文件:C:\ source temp \ GMS_Sandbox_MVC \ GMS_Sandbox_MVC \ Models \ OrganizationRepository.cs> >行:14

来源错误:

Line 12:     {
Line 13:        // GMSSandboxMVCContext context = new GMSSandboxMVCContext();
Line 14:          GMSSandboxMVCContext context = new GMSSandboxMVCContext();
Line 15: 
Line 16:         public IQueryable<Organization> All

任何人都有任何想法可能导致这个?

在错误日志中,它说:

  

错误12类型或命名空间名称'GMSSandboxMVCEntities'不能   找到(你错过了使用指令或程序集   参考?)C:\ source   temp \ GMS_Sandbox_MVC \ GMS_Sandbox_MVC \ Models \ OrganizationRepository.cs

OrganizationRepository.CS

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Web;

namespace GMS_Sandbox_MVC.Models
{ 
public class OrganizationRepository : IOrganizationRepository
{
   // GMSSandboxMVCContext context = new GMSSandboxMVCContext();
     GMSSandboxMVCContext context = new GMSSandboxMVCContext();

    public IQueryable<Organization> All
    {
        get { return context.Organizations; }
    }

    public IQueryable<Organization> AllIncluding(params Expression<Func<Organization, object>>[] includeProperties)
    {
        IQueryable<Organization> query = context.Organizations;
        foreach (var includeProperty in includeProperties) {
            query = query.Include(includeProperty);
        }
        return query;
    }

    public Organization Find(string id)
    {
        return context.Organizations.Find(id);
    }

    public void InsertOrUpdate(Organization organization)
    {
        if (organization.org_nbr == default(string)) {
            // New entity
            context.Organizations.Add(organization);
        } else {
            // Existing entity
            context.Entry(organization).State = EntityState.Modified;
        }
    }

    public void Delete(string id)
    {
        var organization = context.Organizations.Find(id);
        context.Organizations.Remove(organization);
    }

    public void Save()
    {
        context.SaveChanges();
    }
}

public interface IOrganizationRepository
{
    IQueryable<Organization> All { get; }
    IQueryable<Organization> AllIncluding(params Expression<Func<Organization, object>>[] includeProperties);
    Organization Find(string id);
    void InsertOrUpdate(Organization organization);
    void Delete(string id);
    void Save();
}

} 的 OrganizationController.CS

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GMS_Sandbox_MVC.Models;

namespace GMS_Sandbox_MVC.Controllers
{   
    public class OrganizationController : Controller
    {
        private readonly IOrganizationRepository organizationRepository;

        // If you are using Dependency Injection, you can delete the following constructor
        public OrganizationController() : this(new OrganizationRepository())
        {
        }

        public OrganizationController(IOrganizationRepository organizationRepository)
        {
            this.organizationRepository = organizationRepository;
        }

        //
        // GET: /Organizations/

        public ViewResult Index()
        {
            return View(organizationRepository.AllIncluding(organization => organization.Assoc_Role, organization => organization.Grants, organization => organization.Distro_List));
        }

        //
        // GET: /Organizations/Details/5

        public ViewResult Details(string id)
        {
            return View(organizationRepository.Find(id));
        }

        //
        // GET: /Organizations/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Organizations/Create

        [HttpPost]
        public ActionResult Create(Organization organization)
        {
            if (ModelState.IsValid) {
                organizationRepository.InsertOrUpdate(organization);
                organizationRepository.Save();
                return RedirectToAction("Index");
            } else {
                return View();
            }
        }

        //
        // GET: /Organizations/Edit/5

        public ActionResult Edit(string id)
        {
             return View(organizationRepository.Find(id));
        }

        //
        // POST: /Organizations/Edit/5

        [HttpPost]
        public ActionResult Edit(Organization organization)
        {
            if (ModelState.IsValid) {
                organizationRepository.InsertOrUpdate(organization);
                organizationRepository.Save();
                return RedirectToAction("Index");
            } else {
                return View();
            }
        }

        //
        // GET: /Organizations/Delete/5

        public ActionResult Delete(string id)
        {
            return View(organizationRepository.Find(id));
        }

        //
        // POST: /Organizations/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(string id)
        {
            organizationRepository.Delete(id);
            organizationRepository.Save();

            return RedirectToAction("Index");
        }
    }
}

我正在使用here

中的教程

1 个答案:

答案 0 :(得分:0)

我遇到了几乎这个确切的情况,并没有找到太多的指导。如果其他人处于类似的位置,我最终发现了一些值得尝试的事情:

  1. 删除并重新创建您的edmx - 对我来说不起作用,但值得一试!
  2. 确保安装了最新版本的EF - 我对此寄予厚望,但仍然没有运气。
  3. http://jameschambers.com/blog/asp.net-mvcscaffolding-generates-extra-properties-in-views - 我现在有另一个错误(可能是由于complex keys)但我很确定这已经解决了之前的问题!