好的,所以我有点困惑。我有一个mvc 4应用程序已经运行好几周了,直到今天早上我试图运行它(自上次我昨天运行以来没有做任何更改)现在我已经#m;得到此错误说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)
);
答案 0 :(得分:3)
在这种情况下,我认为您可能已经更改了视图模型或sql表中的主键约束;因为必须定义主键才能在EF生成的模型上使用Find()。