2013-10-30 53 views
0

塊引用實體類型[XXX]是不是爲當前上下文

我一直在這個問題現在要去幾天模型的一部分。我試圖使用Visual Studio 2010 MVC2 EF 6.0創建數據庫連接。我可以使用服務器瀏覽器連接到數據庫。

這是我迄今所做的:

  1. 創建的模型:ModelEntities.edmx(連接到SQL Server DB)

2.創建的表我一個型號「M試圖訪問: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)

    using ServiceDesk_Solution.Models;

    命名空間ServiceDesk_Solution.DAL {

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

  2. 我創造了我的視圖控制器(叫我控制器DBController.cs)

    公共類DBController:控制器 { // // GET:/ DB/

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

    }

  3. 最後,我用我的模型 (DB.aspx) ModelEntities.Quickfix 營造濃厚的看法,這是當我得到上下文錯誤(見錯誤和堆棧跟蹤波紋管)

  4. 我的配置文件:

    添加名稱= 「ModelEntities」 的connectionString =「元數據= RES:// /Models.CSCEntities.csdl | res:// /Models.ModelEntities.ssdl | RES://*/Models.ModelEntities.msl;提供商= System.Data.SqlClient的;提供程序連接字符串= "數據源= devsql;初始目錄=模型;堅持安全信息=真;用戶ID =用戶;密碼=密碼; multipleactiveresultsets = TRUE;應用程式=的EntityFramework " 「的providerName =」 System.Data.EntityClient」

沒有更多的錯誤

錯誤消息:

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

說明:執行過程中發生了未處理的異常的當前網絡請求。請查看堆棧跟蹤以獲取有關該錯誤的更多信息以及源代碼的位置。

異常詳細信息: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<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 

回答

2

所以我想通了這個問題。

本教程要求創建所有這些不需要的額外類。基本上所有你需要做的,使實體框架的工作是創建模型,然後在你的控制器中創建對象。

其他一切都在實體模型中,所以當錯誤讀取時:「Quickfix不是模型的一部分」這是真的,因爲我在每個教程中創建一個具有相同名稱的額外類。

當我使用Quickfix上下文創建了一個強大的視圖時,它已經崩潰了,因爲它試圖關聯一個模型中不存在的類。因此,通過刪除所有額外的DAL和Model Context,使用強視圖菜單中顯示的Entity.context創建我的視圖一切正常。

希望這可以幫助一些與我有同樣問題的人。

相關問題