无法创建对象上下文 [英] Unable to create object context

查看:269
本文介绍了无法创建对象上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了无法创建对象上下文的错误使用ASP.NET MVC实体框架的时候。

背景

我每次 POST 到控制器我没有收到回一个响应。我试图直接到控制器 /数据/ GETALL 的方法,并收到此错误:

错误

  

指定命名的连接是   无论是在配置上没有发现,   不意在与所使用的   EntityClient提供商或无效。   说明:未处理的异常   的执行过程中发生   当前Web请求。请检查   堆栈跟踪的更多信息   该错误以及它起源于   在code。

     

异常详细信息:    System.ArgumentException:的   指定命名的连接可以是   在配置上没有发现,不   旨在与所使用的   EntityClient提供商或无效。

$ C C片段$抛出异常:

 公共类TrackItContextCreator {
    公共ObjectContext的创建(){
        ObjectContext的背景下=新的ObjectContext(NAME = TrackItDBEntities);

        context.DefaultContainerName =TrackItDBEntities;

        返回范围内;
    }
}
 

的Web.config

 <添加名称=TrackItDBEntities的connectionString =元数据=水库:// *
/EntityFramework.TrackItDBModel.csdl|res://*/EntityFramework.TrackItDBModel.ssd​​l|res:
//*/EntityFramework.TrackItDBModel.msl;provider=System.Data.SqlClient;provider
 连接字符串=安培; QUOT;数据源=主机;用户ID = myuseracc;密码= ******;
MultipleActiveResultSets =真放; QUOT;的providerName =System.Data.EntityClient/>
 

我能会丢失?

解决方案

从评论,我想我知道是什么问题。

如果该模型是在一个单独的程序,你基本上要确保在的app.config / 网​​页中指定的连接字符串的.config 中的每次的组件使用的模型。我猜,你只需要在它的示范项目。

请参阅<一href="http://stackoverflow.com/questions/1113361/ado-net-entity-connection-string-for-multiple-projects">this问题以另一个例子,和<一href="http://social.msdn.microsoft.com/forums/en-US/adodotnetentityframework/thread/f5904b4d-b2f8-421e-90de-339f93959533/">this线程MSDN 。涉及车型在不同的项目/组件是两个类似的问题。我99%肯定,这是关系到你的问题。

I'm getting the Unable to create object context error when using Entity framework in ASP.NET MVC.

Background

Every time I POST to the controller I'm not getting a response back. I tried going directly to the method of the controller /Data/GetAll, and receive this error:

Error

The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid. 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.ArgumentException: The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid.

Code Snippet that throws exception:

public class TrackItContextCreator {
    public ObjectContext Create() {
        ObjectContext context = new ObjectContext("name=TrackItDBEntities");

        context.DefaultContainerName = "TrackItDBEntities";

        return context;
    }
}

Web.config

<add name="TrackItDBEntities" connectionString="metadata=res://*
/EntityFramework.TrackItDBModel.csdl|res://*/EntityFramework.TrackItDBModel.ssdl|res:
//*/EntityFramework.TrackItDBModel.msl;provider=System.Data.SqlClient;provider
 connection string=&quot;Data Source=host;User ID=myuseracc;Password=******;
MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />

What could I be missing?

解决方案

From the comments, I think I know what the problem is.

If the model is in a separate assembly, you basically have to make sure that the connection string is specified in the app.config/web.config of every assembly that uses the model. I'm guessing that you only have it in the model project.

See this question for another example, and this thread on MSDN. Both similar issues involving models being in separate projects/assemblies. I'm 99% sure that this is related to your issue.

这篇关于无法创建对象上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆