简体   繁体   English

如何在连接字符串中为EF CodeFirst配置ProviderManifestToken

[英]How to configure ProviderManifestToken for EF CodeFirst in a Connection String

Currently, I'm passing a database name to the constructor of DbContext . 目前,我正在将数据库名称传递给DbContext的构造DbContext In the connectionStrings section of my App.Config file, I've added a connection string and specified the provider name: App.Config文件的connectionStrings部分中,我添加了一个连接字符串并指定了提供程序名称:

<connectionStrings>
    <add name="myConnectionString" connectionString="[..]" providerName="System.Data.SqlClient"/>
</connectionStrings>

Now, I want to get the connection string from an other kind of configuration source, but a ProviderIncompatibleException is thrown. 现在,我想从其他类型的配置源中获取连接字符串,但是会抛出ProviderIncompatibleException The exception contains the following message: 异常包含以下消息:

"The provider did not return a providermanifesttoken string".

So, how can I specify the provider name in a connection string? 那么,如何在连接字符串中指定提供者名称? Currently my string contains data source , database and some other configuration settings. 目前,我的字符串包含data sourcedatabase和其他一些配置设置。

Provider is defined by the type of a connection and the connection is created by its connection factory which is set in static property Database.DefaultConnectionFactory . 提供者由连接的类型定义,并且连接由其连接工厂创建,该连接工厂在静态属性Database.DefaultConnectionFactory设置。 Default value of this property is SqlConnectionFactory so unless you are connecting to a different database server (for example SQL Server CE) it should simply work. 此属性的默认值为SqlConnectionFactory因此,除非您连接到其他数据库服务器(例如SQL Server CE),否则它应该可以正常工作。 If it doesn't make sure that your connection string is correct. 如果不确定,请确保您的连接字符串正确。 This exception is sometimes fired if EF cannot connect to SQL server (I think I saw this exception either with invalid credentials, invalid database name or invalid SQL server instance name). 如果EF无法连接到SQL Server,则有时会引发此异常(我认为我看到此异常的原因是凭据无效,数据库名称无效或SQL Server实例名称无效)。

Edit: 编辑:

For completeness: The connection factory is used only if you pass connection string to the context. 为了完整性:仅当您将连接字符串传递给上下文时,才使用连接工厂。 You can also passed whole connection instance and connection factory will not be used. 您还可以传递整个连接实例,并且将不使用连接工厂。 Provider manifest token will be inferred from the type of the connection you will pass to the context. 提供者清单令牌将从您传递给上下文的连接类型中推断出来。

这可能不是您的问题,但是如果您根本无法连接到数据库,也会发生此错误-我们的网络阻塞了端口1433。打开它,错误就消失了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM