使用 POCO 模板获取实体框架的连接字符串

发布于 2024-11-18 17:28:29 字数 610 浏览 4 评论 0原文

我使用实体框架,我需要获取连接字符串,以便构建上下文。

我正在使用 POCO 模板。我的上下文对象有:

string ConnectionString = "name=MyAppConfigConnectionStringPropertyHere"

所以当我尝试构建我的上下文时它说

“在配置中找不到指定的命名连接,不适合与 EntityClient 提供程序一起使用,或者无效”

我在此 答案表明存在 GetConnectionString() 方法。但谷歌搜索并没有引导我找到我需要添加来获取该方法的参考(默认情况下它不在我的项目中)。

所以,我不能认为我是第一个想要使用 POCO 模板获取实体框架连接字符串的人。其他人是如何做到这一点的?我想我可以读取整个 app.config 文件并将其解析出来。但看起来应该没那么难。

任何提示都会很棒!

I use Entity Framework and I need to get my connection string so I can construct a context.

I am using the POCO template. My context object has:

string ConnectionString = "name=MyAppConfigConnectionStringPropertyHere"

So when I try to just construct my context it says

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

I saw in this answer that indicated that there is a GetConnectionString() method. But googling has not lead me to the reference I need to add to get that method (it is not in my project by default).

So, I can't think that I am the first one who wants to get a connection string for entity framework using the POCO templates. How have others done this? I guess I can just read in the whole app.config file and parse it out. But it seems like it should not be that hard.

Any hints would be great!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

手心的温暖 2024-11-25 17:28:29

对于实体框架 6.0,以下代码给出当前的 ConnectionString。

context.Database.Connection.ConnectionString

For entity framework 6.0, following code gives current ConnectionString.

context.Database.Connection.ConnectionString
迟到的我 2024-11-25 17:28:29

对于不使用 EF 4.1 的人,这是我所知道的最简单的方法:

using System.Data.EntityClient;

public static string GetConnectionString(ObjectContext context)
{
    return ((EntityConnection)context.Connection).StoreConnection.ConnectionString;
}

For people not using EF 4.1 here is the easiest way to do it that I know of:

using System.Data.EntityClient;

public static string GetConnectionString(ObjectContext context)
{
    return ((EntityConnection)context.Connection).StoreConnection.ConnectionString;
}
我不吻晚风 2024-11-25 17:28:29

我像这样获得数据库连接

 var connection = (SqlConnection)_context.Database.Connection;

然后从连接中获得 ConnectionString 和您需要的其他信息

I get the DB connection like this

 var connection = (SqlConnection)_context.Database.Connection;

Then from connection you get the ConnectionString and other info you need

不必了 2024-11-25 17:28:29

对于 EF Core,请使用:

context.Database.GetDbConnection().ConnectionString;

For EF Core, use:

context.Database.GetDbConnection().ConnectionString;
你如我软肋 2024-11-25 17:28:29

web.config 中必须存在一个在上下文中使用指定名称的 connectionString 元素:

<configuration>
     <connectionStrings>
          <add name="MyAppConfigConnectionStringPropertyHere"
               providerName="System.Data.SqlClient"
               connectionString="Data Source...." />
     </connectionStrings>
</configuration>

There must be a connectionString-element present in the web.config that the specified name used on the context:

<configuration>
     <connectionStrings>
          <add name="MyAppConfigConnectionStringPropertyHere"
               providerName="System.Data.SqlClient"
               connectionString="Data Source...." />
     </connectionStrings>
</configuration>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文