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

发布于 2024-12-25 22:30:48 字数 5775 浏览 1 评论 0原文

我收到错误:

类型“GMS_Sandbox_MVC.Models.Organization”未映射。查看 该类型尚未通过使用 Ignore 显式排除 方法或 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”无法 被发现(您是否缺少 using 指令或程序集 参考?)C:\source 临时\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");
        }
    }
}

我正在使用 此处

I am getting the error:

The type 'GMS_Sandbox_MVC.Models.Organization' was not mapped. Check
that the type has not been explicitly excluded by using the Ignore
method or NotMappedAttribute data annotation. Verify that the type was
defined as a class, is not primitive, nested or generic, and does not
inherit from EntityObject.
Source File: C:\source temp\GMS_Sandbox_MVC\GMS_Sandbox_MVC\Models\OrganizationRepository.cs> > Line: 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

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

I am using tutorial from here

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

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

发布评论

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

评论(1

纵性 2025-01-01 22:30:48

我几乎遇到了这种情况,但找不到太多指导。我最终发现了一些事情,如果其他人处于类似的位置,可能值得尝试:

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

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!
  2. Make sure you have the latest version of EF installed - I had high hopes in this but still no luck.
  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!
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文