EF 4.1 如何在运行时处理动态连接字符串?

发布于 2024-11-27 11:49:13 字数 791 浏览 1 评论 0原文

我无法使用配置文件,我必须在运行时初始化数据库连接。它是 SQL Server 2008 数据库。

自从我将 EF 更新到较新的 ADO.NET Entity Framework 4.1 - Update 1 后,Database.Connection.ConnectionString 不再对我有用。配置文件中的连接字符串对我来说工作正常,但我需要动态定义它。因此,我找到了通过 DefaultConnectionFactory 执行此操作的其他流行方法,如下所示:

Database.DefaultConnectionFactory = 
    new SqlConnectionFactory("Server=local;Initial Catalog=Db1;Trusted_Connection=True;")

但是,在尝试访问任何 DbSet 派生对象时收到错误消息: 提供程序未返回 ProviderManifestToken 字符串。 ---> System.Data.SqlClient.SqlException:建立与 SQL Server 的连接时发生与网络相关或特定于实例的错误。找不到服务器或无法访问服务器。验证实例名称是否正确以及 SQL Server 是否配置为允许远程连接。 (提供程序:命名管道提供程序,错误:40 - 无法打开与 SQL Server 的连接)

更新:我仅在数据库尚未创建且应该由 EF 重建的情况下才会遇到此问题。如果存在,当我查询 Db 数据时不会发生此类异常。如果我在配置文件中定义完全相同的连接字符串,两种情况都适合我(数据库生成和数据查询)

我做错了什么? 谢谢

I can not use config files, I have to init database connetcion in runtime. It's SQL Server 2008 database.

Since I updated EF to newer ADO.NET Entity Framework 4.1 - Update 1, Database.Connection.ConnectionString does not work for me any more. Connection string in config file works fine for me, but I need to define it dynamically. So I found other popular way to do it by DefaultConnectionFactory like this:

Database.DefaultConnectionFactory = 
    new SqlConnectionFactory("Server=local;Initial Catalog=Db1;Trusted_Connection=True;")

However in the case I got the error message trying to access any DbSet derived object: The provider did not return a ProviderManifestToken string. ---> System.Data.SqlClient.SqlException: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

UPDATE: I experience the issue only in the case database is not yet created and supposed to be rebuild by EF. In case it exists, no such exception occurs as I query Db for data. If I define exactly the same connection string in config files, both cases works fine for me (database generation and data query)

What am I doing wrong?
Thanks

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

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

发布评论

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

评论(2

情深已缘浅 2024-12-04 11:49:13

您可以使用EntityConnectionStringBuilder类来构建上下文的连接字符串,

EntityConnectionStringBuilder entityBuilder =new EntityConnectionStringBuilder();
entityBuilder.Provider = "provider";
entityBuilder.ProviderConnectionString = "connectionString";

并在创建dbContext时传递

 YourContext(entityBuilder.ToString())

You can use EntityConnectionStringBuilder class to build the connection string for the context,

EntityConnectionStringBuilder entityBuilder =new EntityConnectionStringBuilder();
entityBuilder.Provider = "provider";
entityBuilder.ProviderConnectionString = "connectionString";

and pass when creating the dbContext

 YourContext(entityBuilder.ToString())
小梨窩很甜 2024-12-04 11:49:13

您仍然可以使用配置文件,该配置以默认值开始,您可以在运行时更改该值,这就是专门设计的,并且可以设置为应用程序范围或每个用户范围。因此,从一个空的配置设置开始,在运行时构建您的字符串并将其设置到配置文件,然后让您的应用程序从中读取我经常这样做。

明天上班时我将发布一些示例代码来说明我处理此问题的方式

You can still use a config file the config starts with a default and you can change the value at runtime this specifically what hey are designed for and can be set to application wide or per user scope. So start with a empty config setting build your string at runtime and set it to the config file and have your application read from it I do this quite frequently.

I'll post some example code for the way I handle this tomorrow when I get to work

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文