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



块引用

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

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

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

2.为我尝试访问的表创建了一个模型:表.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.模型;

    命名空间 ServiceDesk_Solution.DAL{

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

  2. 我为我的视图创建了一个控制器:(称为我的控制器 DBController.cs)

    公共类 DBController : 控制器{ // 获取:/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. 我的配置文件:

    add name="ModelEntities"

    connectionString="metadata=res:///Models.CSCEntities.csdl| res:///Models.ModelEntities.ssdl| res://*/Models.ModelEntities.msl; provider=System.Data.SqlClient; 提供程序连接字符串=" 数据源=devSQL;初始目录=模型; 持久安全信息=真;用户 ID =用户;密码=密码; 多个活动结果集=真;App=EntityFramework" providerName="System.Data.EntityClient"

不再有错误

错误信息:

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

说明:执行当前 Web 请求期间发生未经处理的异常。请查看堆栈跟踪,了解有关错误及其在代码中起源位置的详细信息。

异常详细信息:System.InvalidOperationException:实体类型快速修复不是当前上下文的模型的一部分。

Source Error: 

Line 14:         public ActionResult DB()
Line 15:         {
Line 16:             db.Quickfix.ToList();
Line 17:             return View();
Line 18:         }

Source File: ***_SolutionControllersDBController.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 ***_SolutionServiceDesk_SolutionControllersDBController.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 

所以我想出了问题所在。

教程要求制作所有这些不需要的额外类。 基本上,要使实体框架正常工作,您需要做的就是创建模型,然后在控制器中创建对象。

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

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

希望这对一些与我有相同问题的人有所帮助。

最新更新