How to configure ProviderManifestToken for EF Code First

后端 未结 10 1097
-上瘾入骨i
-上瘾入骨i 2020-11-27 05:49

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 ge

相关标签:
10条回答
  • 2020-11-27 06:47

    This has proven helpful for me:

    <connectionString="Data Source=WORKSTATION\SQLEXPRESS;Initial Catalog=CodeFirst;User ID=CodeFirst_user;Password=********"/> 
    </connectionStrings> 
    
    0 讨论(0)
  • 2020-11-27 06:53

    I see some comments about oracle above, so here's the code for "EntityFrameworkDbConfiguration" adjusted for oracle:

    internal sealed class EntityFrameworkDbConfiguration : DbConfiguration
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="EntityFrameworkDbConfiguration"/> class.
        /// </summary>
        public EntityFrameworkDbConfiguration()
        {
            this.AddDependencyResolver(Oracle.ManagedDataAccess.EntityFramework.EFOracleProviderServices.Instance);
        }
    }
    
    0 讨论(0)
  • 2020-11-27 06:56

    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);
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-27 06:56

    Changing the Data Source to localhost in the connectionString solved my problem.

    0 讨论(0)
提交回复
热议问题