首页 > 解决方案 > Linqpad 并从外部库中获取配置字符串

问题描述

我刚开始使用 LinqPad (v5),我喜欢它。

但我有一个问题。我从我的 Web 项目中成功导入了 EntityFramework 连接(ObjectContext),当我尝试像这样实例化它时:

var db = new SISTEM.Models.DataModelContainer();

我收到以下错误:

The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid.

我已经向 LinqPad.exe.config 和 LPRun.exe.config 添加了连接字符串(与 web.config 中的相同),但这没有帮助,我得到了同样的错误。

如果我像这样构建 EntityConnectionString:

var ecsb = new EntityConnectionStringBuilder();
ecsb.Provider = "System.Data.SqlClient";
ecsb.ProviderConnectionString = "Persist Security Info=True;Data Source=localhost;initial catalog=BASE;User ID=sa; Password=Elmlodge4;multipleactiveresultsets=True;App=EntityFramework";
ecsb.Metadata = "res://*/Models.DataModel.csdl|res://*/Models.DataModel.ssdl|res://*/Models.DataModel.msl";
var db = new SISTEM.Models.DataModelContainer(ecsb.ToString());

一切都像魅力一样。

大多数时候,就是这样。直到我需要调用一个位于外部库中的函数,该函数又实例化我的 ObjectContext 期望来自配置的连接字符串,它没有被拾取。

我希望我清楚地解释了我的问题,我将不胜感激任何帮助解决它。

更新:

我的网络配置中的片段:

<connectionStrings>
    <add name="DataModelContainer" connectionString="metadata=res://*/Models.DataModel.csdl|res://*/Models.DataModel.ssdl|res://*/Models.DataModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;Persist Security Info=True;Data Source=locahost;initial catalog=BASE;User ID=sa; Password=Elmlodge4;multipleactiveresultsets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
</connectionStrings>

我只是将以上内容复制到 LinqPad 配置中,我不确定是否需要。

米洛斯

标签: c#entity-frameworklinqconnection-stringlinqpad

解决方案


你提到 linqpad.exe.config,但你应该使用 linqpad.config

来自常见问题

我正在引用一个从应用程序配置文件 (app.config) 读取设置的自定义程序集。如何告诉 LINQPad 将其用于我的查询?

最简单的方法是打开查询属性对话框 (F4) 并单击 app.config 选项卡。

或者,您可以在与 LINQPad.exe 相同的文件夹中创建一个名为 linqpad.config 的文件。您在此处放置的任何内容都适用于所有查询(除非您在上面的对话框中覆盖它)。不要将 linqpad.config 与 linqpad.exe.config 混淆:

linqpad.config 用于您的查询。

linqpad.exe.config 用于 LINQPad GUI


推荐阅读