EF 4.1 如何在运行时处理动态连接字符串?
我无法使用配置文件,我必须在运行时初始化数据库连接。它是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用EntityConnectionStringBuilder类来构建上下文的连接字符串,
并在创建dbContext时传递
You can use
EntityConnectionStringBuilder
class to build the connection string for the context,and pass when creating the dbContext
您仍然可以使用配置文件,该配置以默认值开始,您可以在运行时更改该值,这就是专门设计的,并且可以设置为应用程序范围或每个用户范围。因此,从一个空的配置设置开始,在运行时构建您的字符串并将其设置到配置文件,然后让您的应用程序从中读取我经常这样做。
明天上班时我将发布一些示例代码来说明我处理此问题的方式
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