gpt4 book ai didi

c# - 无法创建对象上下文

转载 作者:太空狗 更新时间:2023-10-30 00:46:51 25 4
gpt4 key购买 nike

在 ASP.NET MVC 中使用 Entity 框架时出现Unable to create object context 错误。

背景

每次我POST 到 Controller 时,我都没有收到响应。我尝试直接转到 Controller /Data/GetAll 的方法,并收到此错误:

错误

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.

抛出异常的代码片段:

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" />

我可能遗漏了什么?

最佳答案

从评论中,我想我知道问题出在哪里了。

如果模型在单独的程序集中,您基本上必须确保连接字符串在 app.config/web.config 中指定>每个 使用该模型的程序集。我猜你只在模型项目中有它。

参见 this question再举一个例子,this thread on MSDN .这两个类似的问题都涉及模型在单独的项目/组件中。我 99% 确定这与您的问题有关。

关于c# - 无法创建对象上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2233897/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com