如何为 EF Code First 配置 ProviderManifestToken [英] How to configure ProviderManifestToken for EF Code First
问题描述
我有一个使用 EF 代码优先的 asp.net MVC3 项目.对于我的单元测试,我一直使用 SQL Server CE 4.0 和 SQL Server 2008 Express.两者都与 EF 完美配合,按预期生成了我的数据库.
I have a asp.net MVC3 project using EF code-first. For my unit testing I have been using SQL Server CE 4.0 and SQL Server 2008 Express. Both have worked perfectly with EF generating my database as expected.
但是,当我在单元测试之外运行我的应用程序并将其指向我的连接字符串时,我收到错误
However, when I run my application outside of a unit test and point it at my connection strings I get the error
ProviderIncompatibleException: 提供者没有返回 ProviderManifestToken 字符串
ProviderIncompatibleException: The provider did not return a ProviderManifestToken string
我已阅读有关此的 MS 文档,这似乎是 EF 模型生成的 SqlVersion
标记.问题是我使用的是代码优先方法,所以我没有 .edmx
文件,也不知道将元数据信息指向哪里,因为数据库尚未生成.
I have read the MS documentation on this and it appears this is a SqlVersion
token that the EF model generates. The problem is that I am using the code first approach so I have no .edmx
file nor do I know where to point my metadata info to because the db hasn't been generated yet.
我知道我的连接字符串就 db name、username 和 pass 而言是正确的,因为将它们更改为错误的值会引发预期的错误.不知道从哪里开始.
I know my connection strings as far as db name, username, and pass are correct because changing them to wrong values throws the expected error. Not sure where to begin.
谢谢.
这是我的连接字符串:
<connectionStrings>
<add
name="SqlConnection"
providerName="System.Data.SqlClient"
connectionString="Data Source=WORKSTATIONSQLEXPRESS;Initial Catalog=CodeFirst;Integrated Security=False;
Persist Security Info=False;User ID=CodeFirst_user;Password=password1;Connect Timeout=120;MultipleActiveResultSets=True;"/>
</connectionStrings>
推荐答案
如果您使用的是 EF 6(刚刚发布),您还有一个选择.
If you're using EF 6 (just released) you have an alternative.
您可以使用新的依赖解析功能来注册IManifestTokenResolver
(在本预览文档中描述为IManifestTokenService
).
You can use the new dependency resolution feature to register an implementation of IManifestTokenResolver
(described in this preview documentation as IManifestTokenService
).
本文提供了有关如何使用 DbConfiguration
的更多信息.最简单的使用方法是这样的:
This article gives a bit more information on how to use DbConfiguration
. The easiest way to use it is like this:
DbConfigurationType(typeof(EntityFrameworkDbConfiguration))]
public class MyContextContext : DbContext
{
}
此示例在为 SQL Server 连接构建元数据时避免了对数据库的任何访问,并自动指定了 SQL Server 2005 兼容性.
This example avoids any trip to the database when building the metadata for SQL Server connections, and automatically specifies SQL Server 2005 compatability.
using System.Data.Common;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.Infrastructure.DependencyResolution;
using System.Data.SqlClient;
/// <summary>
/// A configuration class for SQL Server that specifies SQL 2005 compatability.
/// </summary>
internal sealed class EntityFrameworkDbConfiguration : DbConfiguration
{
/// <summary>
/// The provider manifest token to use for SQL Server.
/// </summary>
private const string SqlServerManifestToken = @"2005";
/// <summary>
/// Initializes a new instance of the <see cref="EntityFrameworkDbConfiguration"/> class.
/// </summary>
public EntityFrameworkDbConfiguration()
{
this.AddDependencyResolver(new SingletonDependencyResolver<IManifestTokenResolver>(new ManifestTokenService()));
}
/// <inheritdoc />
private sealed class ManifestTokenService : IManifestTokenResolver
{
/// <summary>
/// The default token resolver.
/// </summary>
private static readonly IManifestTokenResolver DefaultManifestTokenResolver = new DefaultManifestTokenResolver();
/// <inheritdoc />
public string ResolveManifestToken(DbConnection connection)
{
if (connection is SqlConnection)
{
return SqlServerManifestToken;
}
return DefaultManifestTokenResolver.ResolveManifestToken(connection);
}
}
}
这篇关于如何为 EF Code First 配置 ProviderManifestToken的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!