0

块引用

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

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

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

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

    使用 ServiceDesk_Solution.Models;

    命名空间 ServiceDesk_Solution.DAL{

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

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

    公共类 DBController : Controller { // // 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="metadata=res:// /Models.CSCEntities.csdl| res:// /Models.ModelEntities.ssdl| res://*/Models.ModelEntities.msl; provider=System.Data .SqlClient; 提供者连接字符串="数据源=devsql;初始目录=Model; 坚持安全信息=真;用户ID=用户;密码=密码;多个活动结果集=真;App=EntityFramework"" providerName="System.Data.EntityClient"

没有更多的错误

错误信息:

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

说明:执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

异常详细信息: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 

4

1 回答 1

3

所以我发现了问题所在。

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

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

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

希望这可以帮助一些和我有同样问题的人。

于 2013-11-09T17:48:28.463 回答