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 been going at this problem for a few days now. I'm trying to create a db connection using Visual Studio 2010 MVC2 EF 6.0. I can connect to the DB using the server explorer.

Here is what I done so far:

  1. Created a Model: ModelEntities.edmx (connects to a SQL Server DB)

2. Created a Model for the table I'm trying 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. Created a DAL folder and added my context to it: (ModelEntitesContext.cs )

    using ServiceDesk_Solution.Models;

    namespace ServiceDesk_Solution.DAL{

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

  2. I created a controller for my View: (Called my controller DBController.cs)

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

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

    }

  3. Finally I create a strong view using my Model (DB.aspx) ModelEntities.Quickfix and this is when I get the context error (see error and stack trace bellow)

  4. My config 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;initial catalog=Model; persist security info=True;user id=user;password=password; multipleactiveresultsets=True; App=EntityFramework"" providerName="System.Data.EntityClient"

No more error

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. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: The entity type Quickfix 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 

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

Accepted Answer

So I figured out the problem.

The tutorials ask to make all these extra classes that are not needed. Basically all you need to do, to make the entity framework work is create the model and then create the object in your controler.

Everything else is in the entity model, so when the error read: "Quickfix is not part of the model" it was true because I was creating an extra class with the same name per the tutorials.

When I created a strong view using the Quickfix context it blew up, because it was trying to associate a class that did not exists in the model. So by deleting all the extra DAL and Model Context, creating my view using the Entity.context that showed up in the Strong view menu everything worked just fine.

Hope this helps a few people having the same issue 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