Entity type [xxx] is not part of the model for the current context

Blockquote

I have been dealing with this problem for several days. I am trying to create a db connection using Visual Studio 2010 MVC2 EF 6.0. I can connect to the database using the server explorer.

Here is what I have done so far:

  • Model created: ModelEntities.edmx (connects to SQL Server database)

2. Created a model for the table I'm trying to access: Table.cs (has all public elements)

public class Quickfix { public int FIX_ID { get; set; } public string NAME { get; set; } public string TYPE { get; set; } public string DESCRIPTION { get; set; } } 
  • Created a DAL folder and added its context to it: (ModelEntitesContext.cs)

    using ServiceDesk_Solution.Models;

    ServiceDesk_Solution.DAL namespace {

      public class ModelEntitiesContext : DbContext { public ModelEntitiesContext() : base("ModelEntities") { } public DbSet<Quickfix> Quickfixs { get; set; } } } 

    hit>

  • I created a controller for my view: (My DBController.cs controller is called)

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

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

    }

  • Finally, I create a strong view using my Model (DB.aspx) ModelEntities.Quickfix , and this is when I get a contextual error (see below error and stack stack) strike>

  • My configuration file:

    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; start directory = model; safety information = True; user id = user; password = password; multipleactiveresultsets = True; App = EntityFramework "" ProviderName = "System.Data.EntityClient"

More bugs

Error message:

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

Description: An unhandled exception occurred during the execution of the current web request. View the stack trace for more information about the error and its occurrence in the code.

Exception Details: System.InvalidOperationException: The Quickfix object type is not part of the model for the current context.

 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<TResult>.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.<InvokeActionMethodWithFilters>b__a() +127 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +436 System.Web.Mvc.<>c__DisplayClassf.<InvokeActionMethodWithFilters>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.<BeginProcessRequest>b__4() +65 System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +44 System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>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 

Strike>

0
source share
1 answer

So, I understood the problem.

In the textbooks, please do all these extra classes that are not needed. Basically, all you need to do to work with the entity infrastructure is creating a model and then creating an object in your controller.

Everything else is in the entity model, so when the error reads: “Quickfix is ​​not part of the model”, this was true because I created an extra class with the same name in the tutorials.

When I created a strong view using the Quickfix context, it exploded because it was trying to link a class that did not exist in the model. Thus, removing all the additional DALs and the model context, creating my view using Entity.context, which appeared in the Strong View menu, everything worked fine.

Hope this helps a few people having the same problem as me.

+2
source

All Articles