如何配置ProviderManifestToken为EF代码首先 [英] How to configure ProviderManifestToken for EF Code First

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

问题描述

我有一个使用EF代码优先的asp.net MVC3项目。对于我的单元测试,我一直在使用SQL Server CE 4.0和SQL Server 2008 Express。两者都完美地与EF一起工作,如预期的那样生成我的数据库。



但是,当我在单元测试之外运行我的应用程序并将其指向我的连接字符串时,我得到错误


ProviderIncompatibleException:提供者未返回ProviderManifestToken字符串


我已经阅读了MS文档,这似乎是EF模型生成的一个 SqlVersion 令牌。问题是我正在使用代码第一种方法,所以我没有 .edmx 文件,也不知道在哪里指向我的元数据信息,因为db尚未生成



我知道我的连接字符串到db name,username和pass是正确的,因为将它们更改为错误的值会抛出预​​期的错误。不知道从哪里开始。



谢谢。



这是我的连接字符串:

 < connectionStrings> 
< add
name =SqlConnection
providerName =System.Data.SqlClient
connectionString =Data Source = WORKSTATION\SQLEXPRESS; Initial Catalog = CodeFirst; Integrated Security = False;
Persist Security Info = False; User ID = CodeFirst_user; Password = password1; Connect Timeout = 120; MultipleActiveResultSets = True;/>
< / connectionStrings>


解决方案

如果您使用的是EF 6(刚刚发布)你有另一种选择。



依赖解决方案



您可以使用新的依赖性解决功能注册 IManifestTokenResolver 的实现(在< a href =http://msdn.microsoft.com/en-us/data/jj680697>此预览文档为 IManifestTokenService )。



这篇文章给出了更多的信息关于如何使用 DbConfiguration 。使用它的最简单的方法就是这样:

  DbConfigurationType(typeof(EntityFrameworkDbConfiguration))] 
public class MyContextContext: DbContext
{
}

此示例避免在构建时访问数据库SQL Server连接的元数据,并自动指定SQL Server 2005兼容性。

  using System.Data.Common; 
使用System.Data.Entity;
使用System.Data.Entity.Infrastructure;
使用System.Data.Entity.Infrastructure.DependencyResolution;
使用System.Data.SqlClient;

///< summary>
///指定SQL 2005兼容性的SQL Server的配置类。
///< / summary>
内部密封类EntityFrameworkDbConfiguration:DbConfiguration
{
///< summary>
///用于SQL Server的提供程序清单令牌。
///< / summary>
private const string SqlServerManifestToken = @2005;

///< summary>
///初始化< see cref =EntityFrameworkDbConfiguration/>的新实例类。
///< / summary>
public EntityFrameworkDbConfiguration()
{
this.AddDependencyResolver(new SingletonDependencyResolver< IManifestTokenResolver>(new ManifestTokenService()));
}

///< inheritdoc />
私人密封类ManifestTokenService:IManifestTokenResolver
{
///< summary>
///默认的令牌解析器。
///< / summary>
private static readonly IManifestTokenResolver DefaultManifestTokenResolver = new DefaultManifestTokenResolver();

///< inheritdoc />
public string ResolveManifestToken(DbConnection connection)
{
if(connection is SqlConnection)
{
return SqlServerManifestToken;
}

return DefaultManifestTokenResolver.ResolveManifestToken(connection);
}
}
}


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: The provider did not return a ProviderManifestToken string

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.

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.

Thanks.

Here is my connection string:

<connectionStrings>
  <add
    name="SqlConnection"
    providerName="System.Data.SqlClient"
    connectionString="Data Source=WORKSTATION\SQLEXPRESS;Initial Catalog=CodeFirst;Integrated Security=False;
    Persist Security Info=False;User ID=CodeFirst_user;Password=password1;Connect Timeout=120;MultipleActiveResultSets=True;"/>
</connectionStrings>

解决方案

If you're using EF 6 (just released) you have an alternative.

Dependency Resolution

You can use the new dependency resolution feature to register an implementation of IManifestTokenResolver (described in this preview documentation as IManifestTokenService).

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
{
}

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);
        }
    }
}

这篇关于如何配置ProviderManifestToken为EF代码首先的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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