无法创建对象上下文 [英] Unable to create object context
问题描述
在ASP.NET MVC中使用Entity框架时,我得到无法创建对象上下文
错误。
I'm getting the Unable to create object context
error when using Entity framework in ASP.NET MVC.
每当我将 POST
给控制器时,我没有收到回复。我尝试直接访问控制器的方法 / Data / GetAll
,并收到此错误:
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:
在配置中找不到指定的命名连接是
,
不打算与$ b一起使用$ b EntityClient提供者,或无效。
描述:执行
当前Web请求期间发生未处理的异常
。请查看
堆栈跟踪,了解有关
错误的更多信息,以及
中的代码。
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.
异常详细信息:
System.ArgumentException:
指定的命名连接是在配置中找不到
,而不是
用于
EntityClient提供者,或无效。
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
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="Data Source=host;User ID=myuseracc;Password=******;
MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />
我可能缺少什么?
推荐答案
从评论中,我想我知道问题是什么。
From the comments, I think I know what the problem is.
如果模型在单独的程序集中,以确保在 app.config
/ web.config
中指定连接字符串 / em>使用模型的程序集。我猜你只有在模特儿项目中。
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.
请参阅这个问题为另一个例子,而 MSDN上的这个线程。涉及模型的两个类似问题都在单独的项目/组件中。我99%确定这与您的问题有关。
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屋!