如何在连接字符串配置为ProviderManifestToken EF CodeFirst [英] How to configure ProviderManifestToken for EF CodeFirst in a Connection String

查看:3040
本文介绍了如何在连接字符串配置为ProviderManifestToken EF CodeFirst的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我传递一个数据库名称的DbContext 的构造函数。在我的 App.Config中文件中的是connectionStrings 部分,我添加了一个连接字符串和指定的供应商名称:

Currently, I'm passing a database name to the constructor of DbContext. In the connectionStrings section of my App.Config file, I've added a connection string and specified the provider name:

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

现在,我想从一个其他类型的配置源的连接字符串,但 ProviderIncompatibleException 被抛出。该异常包含以下信息:

Now, I want to get the connection string from an other kind of configuration source, but a ProviderIncompatibleException is thrown. 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.

推荐答案

提供由连接类型定义,并通过其在静态属性设定其连接工厂创建的连接 Database.DefaultConnectionFactory 。此属性的默认值为 SqlConnectionFactory 所以,除非你连接到不同的数据库服务器(例如SQL Server CE的),它应该只是工作。如果它不请确保您连接字符串是正确的。如果EF无法连接到SQL服务器,此异常有时解雇(我想我看到这个异常要么无效的信用凭证无效的数据库名称或无效的SQL Server实例的名称)。

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. 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. 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).

编辑:

有关完整性:连接工厂只有当你传递连接字符串上下文中使用。也可以通过整体连接实例和连接工厂将不被使用。提供商明证将从你将传递到上下文连接的类型来推断。

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.

这篇关于如何在连接字符串配置为ProviderManifestToken EF CodeFirst的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆