0

好的,所以我有点困惑。我有一个 mvc 4 应用程序,它已经运行了好几个星期,直到今天早上我尝试运行它(自昨天上次运行它以来没有进行任何更改),现在我收到这个错误说EntityType 'MyViewModel' 没有定义键。为此 EntityType 定义一个键。该错误在我的通用存储库类中弹出:

public virtual TEntity GetByID(object id)
    {
        return dbSet.Find(id);
    }

我的应用程序使用 EF 代码优先方法生成数据库,当我查看表定义时,它会将 ID 属性识别为我的主键,所以我不知道这里发生了什么。

GenericRepository.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.Data;
using SourceMvc.Models;
using SourceMvc.DAL;
using System.Linq.Expressions;

namespace SourceMvc.DAL
{
public class GenericRepository<TEntity> where TEntity : class
{
    internal StqmContext context;
    internal DbSet<TEntity> dbSet;

    public GenericRepository(StqmContext context)
    {
        this.context = context;
        this.dbSet = context.Set<TEntity>();
    }

    public virtual IEnumerable<TEntity> Get(
        Expression<Func<TEntity, bool>> filter = null,
        Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
        string includeProperties = "")
    {
        IQueryable<TEntity> query = dbSet;

        if (filter != null)
        {
            query = query.Where(filter);
        }

        foreach (var includeProperty in includeProperties.Split
            (new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
        {
            query = query.Include(includeProperty);
        }

        if (orderBy != null)
        {
            return orderBy(query).ToList();
        }
        else
        {
            return query.ToList();
        }
    }
    public virtual TEntity GetByID(object id)
    {
        return dbSet.Find(id);
    }

    public virtual void Insert(TEntity entity)
    {
        dbSet.Add(entity);
    }

    public virtual void Delete(object id)
    {
        TEntity entityToDelete = dbSet.Find(id);
        Delete(entityToDelete);
    }

    public virtual void Delete(TEntity entityToDelete)
    {
        if (context.Entry(entityToDelete).State == EntityState.Detached)
        {
            dbSet.Attach(entityToDelete);
        }
        dbSet.Remove(entityToDelete);
    }

    public virtual void Update(TEntity entityToUpdate)
    {
        dbSet.Attach(entityToUpdate);
        context.Entry(entityToUpdate).State = EntityState.Modified;
    }

}
}

我的视图模型:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SourceMvc.Models;

namespace SourceMvc.DAL
{
public class QuotesViewModel
{
    //properties
    public General_Info     General_Info    { get; private set; }

    // Constructors

    public QuotesViewModel()
    {

    }

    public QuotesViewModel(General_Info general_info)
    {
        General_Info = general_info;
    }

抛出错误时调用的控制器:

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using SourceMvc.Models;
using SourceMvc.DAL;

namespace SourceMvc.Controllers
{
    public class VendorController : Controller
    {

        UnitOfWork unitOfWork = new UnitOfWork();

        internal IGeneral_Info_Repository general_info_repository;

        public VendorController()
        {
            this.general_info_repository = new General_Info_Repository(new StqmContext());
        }

        internal VendorController(IGeneral_Info_Repository general_info_repository)
        {
            this.general_info_repository = general_info_repository;
        }

        //
        // GET: /Vendor/

        [HttpGet]
        public ActionResult InitiateQuote()
        {
            return View();

        }

        //
        // POST: /Vendor/
        [HttpPost]
        public ActionResult InitiateQuote(int id = 0)
        {
            /*
             * If the form is filled out correctly 
             * (i.e. an existing quote id # is entered)
             * redirect the user to Vendor/Details/id
             */
            if (ModelState.IsValid)
            {
                int ids = id;
                return RedirectToAction("Overview", "Vendor", new { id = ids });
            }

            return View();
        }

        ////WHEN I SUBIT THE QUOTE ID AND THE OVERVIEW PAGE IS SUPPOSED TO LOAD,
        ///THAT'S WHEN I GET THE ERROR
        //
        // GET: /Vendor/Overview/id

        public ActionResult Overview(int id = 0)
        {
            General_Info general_info = unitOfWork.General_Info_Repository.GetByID(id);
            return View(new QuotesViewModel(general_info));
        }

老实说,我不知道是什么原因造成的,因为就像我说的,这在昨天有效,并且已经工作了数周。提前感谢任何试图提供帮助的人。

这是表格:

CREATE TABLE [dbo].[General_Info] (
[ID]              INT            IDENTITY (1, 1) NOT NULL,
[Open_Quote]      DATETIME       NOT NULL,
[Customer_Name]   NVARCHAR (MAX) NULL,
[OEM_Name]        NVARCHAR (MAX) NULL,
[Qty]             INT            NOT NULL,
[Quote_Num]       NVARCHAR (MAX) NULL,
[Fab_Drawing_Num] NVARCHAR (MAX) NULL,
[Rfq_Num]         NVARCHAR (MAX) NULL,
[Rev_Num]         NVARCHAR (MAX) NULL,
CONSTRAINT [PK_dbo.General_Info] PRIMARY KEY CLUSTERED ([ID] ASC)
);
4

1 回答 1

3

在这种情况下,我认为您可能已经更改了视图模型或 sql 表中的主键约束;因为必须定义主键才能在 EF 生成的模型上使用 Find()。

于 2013-03-22T16:26:01.613 回答