简体   繁体   English

尝试在浏览器中安装支架后未映射类型

[英]The type was not mapped after trying to scaffolded controller in browser

I am getting the error: 我收到错误消息:

The type 'GMS_Sandbox_MVC.Models.Organization' was not mapped. 未映射类型“ GMS_Sandbox_MVC.Models.Organization”。 Check that the type has not been explicitly excluded by using the Ignore method or NotMappedAttribute data annotation. 通过使用Ignore方法或NotMappedAttribute数据批注检查类型是否未明确排除。 Verify that the type was defined as a class, is not primitive, nested or generic, and does not inherit from EntityObject. 验证类型是否定义为类,不是原始的,嵌套的或泛型的,并且没有从EntityObject继承。 Source File: C:\\source temp\\GMS_Sandbox_MVC\\GMS_Sandbox_MVC\\Models\\OrganizationRepository.cs> > Line: 14 源文件:C:\\ source temp \\ GMS_Sandbox_MVC \\ GMS_Sandbox_MVC \\ Models \\ OrganizationRepository.cs>>行:14

Source Error: 源错误:

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

Anybody have any ideas what might be causing this? 有人有什么想法可能导致这种情况吗?

In error log it says: 错误日志中显示:

Error 12 The type or namespace name 'GMSSandboxMVCEntities' could not be found (are you missing a using directive or an assembly reference?) C:\\source temp\\GMS_Sandbox_MVC\\GMS_Sandbox_MVC\\Models\\OrganizationRepository.cs 错误12找不到类型或名称空间名称'GMSSandboxMVCEntities'(您是否缺少using指令或程序集引用?)C:\\ source temp \\ GMS_Sandbox_MVC \\ GMS_Sandbox_MVC \\ Models \\ OrganizationRepository.cs

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 } 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");
        }
    }
}

I am using tutorial from here 我正在从这里使用教程

I ran into almost this exact scenario and couldn't find much guidance. 我几乎碰到了这种确切情况,找不到太多指导。 I eventually found a couple of things which could be worth trying if anyone else is in a similar position: 我最终发现如果其他人处于相似的位置,可能值得尝试的几件事:

  1. Delete and recreate your edmx - didn't work for me but worth a go! 删除并重新创建您的edmx-不适用于我,但值得一试!
  2. Make sure you have the latest version of EF installed - I had high hopes in this but still no luck. 确保您已安装最新版本的EF-我对此寄予厚望,但仍然没有运气。
  3. http://jameschambers.com/blog/asp.net-mvcscaffolding-generates-extra-properties-in-views - I have another error now (possibly due to complex keys ) but I'm pretty sure this has solved the previous one! http://jameschambers.com/blog/asp.net-mvcscaffolding-generates-extra-properties-in-views-我现在有另一个错误(可能是由于复杂的键 ),但是我很确定这已经解决了上一个错误!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在我搭建 Identity 后没有错误的空白页面 - Blank page with no error after i scaffolded Identity 类型“”未映射 - The type '' was not mapped .NET Core 2.2无法访问Controller,但可以使用脚手架登录 - .NET Core 2.2 Unable to reach Controller but scaffolded Login works 在带有EF的脚手架控制器ASP.NET MVC的索引方法中添加where子句 - Adding where clause to index method of scaffolded controller ASP.NET MVC with EF c# 使用“添加新脚手架项目”的“MVC Controller with Views”代码生成器出错 - c# Error With code Generator For 'MVC Controller with Views' using 'Add New Scaffolded Item' 从 .NET 5 升级到 .NET 6 后,脚手架身份页面中的自定义 Bootstrap 4 UI 损坏 - Custom Bootstrap 4 UI in scaffolded Identity pages broken after upgrading from .NET 5 to .NET 6 尝试创建X类型的控制器时发生错误。请确保该控制器具有无参数构造函数 - An error occurred when trying to create a controller of type X. Make sure that the controller has a parameterless constructor 尝试创建“ XXXXController”类型的控制器时发生错误。 确保控制器具有无参数的公共构造函数 - An error occurred when trying to create a controller of type 'XXXXController'. Make sure that the controller has a parameterless public constructor 最小化浏览器后无法在输入框中输入 - Can't Type in input box after minimizing browser 实体框架错误“实体类型未映射。” - Entity framework error "Entity type is not mapped."
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM