实体类型不是当前上下文的模型的一部分

大段引用

我现在已经解决了这个问题几天了。 我正在尝试使用Visual Studio 2010 MVC2 EF 6.0创建数据库连接。 我可以使用服务器资源管理器连接到数据库。

这是我到目前为止所做的:

  1. 创建了一个Model:ModelEntities.edmx(连接到SQL Server DB)

2.为我正在尝试访问的表创建了一个Model:Table.cs(包含所有公共成员)

public class Quickfix { public int FIX_ID { get; set; } public string NAME { get; set; } public string TYPE { get; set; } public string DESCRIPTION { get; set; } } 
  1. 创建了一个DAL文件夹并将其上下文添加到其中:(ModelEntitesContext.cs)

    使用ServiceDesk_Solution.Models;

    namespace ServiceDesk_Solution.DAL {

      public class ModelEntitiesContext : DbContext { public ModelEntitiesContext() : base("ModelEntities") { } public DbSet Quickfixs { get; set; } } } 
  2. 我为我的View创建了一个控制器:(称为我的控制器DBController.cs)

    public class DBController:Controller {// // GET:/ DB /

     ModelEntitiesContext db = new ModelEntitiesContext (); ModelEntities db = new ModelEntities(); public ActionResult DB() { return View(db.Quickfix.ToList();); } 

    }

  3. 最后,我使用我的模型(DB.aspx) ModelEntities.Quickfix创建一个强大的视图,这是当我得到上下文错误时(请参阅下面的错误和堆栈跟踪)

  4. 我的配置文件:

    add name =“ModelEntities”connectionString =“metadata = res:// /Models.CSCEntities.csdl | res:// /Models.ModelEntities.ssdl | res://*/Models.ModelEntities.msl; provider = System.Data .SqlClient; provider connection string =“data source = devsql; initial catalog = Model; persist security info = True; user id = user; password = password; multipleactiveresultsets = TRUE; App = EntityFramework“”providerName =“System.Data.EntityClient”

没有更多的错误

错误信息:

 The entity type Quickfix is not part of the model for the current context. 

描述:执行当前Web请求期间发生未处理的exception。 请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

exception详细信息:System.InvalidOperationException:实体类型Quickfix不是当前上下文的模型的一部分。

 Source Error: Line 14: public ActionResult DB() Line 15: { Line 16: db.Quickfix.ToList(); Line 17: return View(); Line 18: } Source File: ***_Solution\Controllers\DBController.cs Line: 16 Stack Trace: [InvalidOperationException: The entity type Quickfix is not part of the model for the current context.] System.Data.Entity.Internal.InternalContext.UpdateEntitySetMappingsForType(Type entityType) +191 System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) +46 System.Data.Entity.Internal.Linq.InternalSet`1.Initialize() +125 System.Data.Entity.Internal.Linq.InternalSet`1.GetEnumerator() +33 System.Data.Entity.Infrastructure.DbQuery`1.System.Collections.Generic.IEnumerable.GetEnumerator() +100 System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) +315 System.Linq.Enumerable.ToList(IEnumerable`1 source) +58 ServiceDesk_Solution.Controllers.DBController.DB() in ***_Solution\ServiceDesk_Solution\Controllers\DBController.cs:16 lambda_method(Closure , ControllerBase , Object[] ) +96 System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +51 System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +409 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +52 System.Web.Mvc.c__DisplayClassd.b__a() +127 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +436 System.Web.Mvc.c__DisplayClassf.b__c() +61 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +305 System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +830 System.Web.Mvc.Controller.ExecuteCore() +136 System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +111 System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +39 System.Web.Mvc.c__DisplayClass8.b__4() +65 System.Web.Mvc.Async.c__DisplayClass1.b__0() +44 System.Web.Mvc.Async.c__DisplayClass8`1.b__7(IAsyncResult _) +42 System.Web.Mvc.Async.WrappedAsyncResult`1.End() +141 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +54 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40 System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +52 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +38 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8981789 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184 -------------------------------------------------------------------------------- Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.272 

所以我想出了问题所在。

教程要求制作所有这些不需要的额外课程。 基本上你需要做的就是让entity framework工作是创建模型,然后在你的控制器中创建对象。

其他所有内容都在实体模型中,因此当错误读取时:“Quickfix不是模型的一部分”,这是真的,因为我根据教程创建了一个具有相同名称的额外类。

当我使用Quickfix上下文创建一个强大的视图时,它会爆炸,因为它试图关联模型中不存在的类。 因此,通过删除所有额外的DAL和模型上下文,使用强视图菜单中显示的Entity.context创建我的视图一切正常。

希望这有助于一些人和我有同样的问题。