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

ado.net asp.net-mvc c# entity-framework poco

Question

Blockquote

I've been working on this issue for several days now. Making a database connection in Visual Studio 2010 MVC2 EF 6.0 is what I'm attempting. Using the server explorer, I can connect to the database.

Here is my current work:

  1. a Model called ModelEntities.edmx was made (connects to a SQL Server DB)

2. Made a model for the database table I'm attempting to access: Table.cs ( has all the public members)

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. I made a DAL folder and put my context in it: (ModelEntitesContext.cs)

    service desk solution models;

    ServiceDesk Solution.DAL namespace

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

  2. For my view, I wrote a controller: (I called DBController.cs, my controller)

    Public class DBController: Controller GET: /DB using the following code.

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

    }

  3. Finally, I use my Model (DB.aspx) ModelEntities to generate a robust view. Fix-it-quick I then experience a context issue at this point (see error and stack trace bellow)

  4. config file I use:

    Name="ModelEntities" providerName="System.Data.EntityClient" connectionString="metadata=res:/res:/ /Models.CSCEntities.csdl/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;

No more mistakes

Mistake Message:

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

The current web request encountered an unhandled exception while it was being processed. To learn more about the error and where in the code it first appeared, please check the stack trace.

Exception Information: System. InvalidOperation The entity type is an exception. Quickfix is not included in the model in this situation.

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 

1
0
11/13/2013 1:31:08 AM

Accepted Answer

So I discovered the issue.

The instructions request the creation of numerous unnecessary additional classes. In order for the entity framework to function, all you really need to do is construct the model, followed by the object in your controler.

Everything else is contained in the entity model, thus when the error message stated that Quickfix was not a model component, it was accurate because I had added a second class with the same name in accordance with the tutorials.

It blew up when I tried to create a strong view with the Quickfix context since it was attempting to associate a class that wasn't present in the model. So everything was good once I deleted all the additional DAL and Model Context and created my view using the Entity.context that appeared in the Strong view menu.

I hope this aids a few others who are experiencing the same problem as me.

3
11/9/2013 5:48:28 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow