好吧,我有点糊涂了。我有一个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;
}
}
}
my View Model:
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)
);
在这种情况下,我认为你可能已经改变了你的视图模型或sql表的主键约束;因为必须定义主键才能在EF生成的模型上使用Find()。