【问题标题】:Entity Framework 6 with SQL Server 2012 gives System.Data.Entity.Core.ProviderIncompatibleException带有 SQL Server 2012 的实体框架 6 提供 System.Data.Entity.Core.ProviderIncompatibleException
【发布时间】:2013-09-03 04:14:06
【问题描述】:

我有 Visual Studio 2012,我正在使用 EF 6 的实体框架堆栈。我做的一切都是正确的,但是在添加迁移时我得到了错误。

System.Data.Entity.Core.ProviderIncompatibleException

这里是类

public class Order
{
    public virtual int OrderID { get; set; }
}

上下文文件

public ShoppingCartContext() : base("ShoppingCartDb")
{
        Database.SetInitializer<ShoppingCartContext>(new DropCreateDatabaseAlways<ShoppingCartContext>());
}

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
        #region Entity Framework 6 RC-1
        modelBuilder.Properties().Where(x => x.Name == x.DeclaringType.ToString() + "ID")
                .Configure(x => x.IsKey());

        modelBuilder.Properties<DateTime>()
                .Configure(x => x.HasColumnType("datetime2"));
        #endregion

        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        base.OnModelCreating(modelBuilder);
    }

以及用于连接字符串的 web.config 文件部分

<connectionStrings>
   <add name="ShoppingCartDb" 
        connectionString="Server=Localhost;Database=ShoppingCartEfDb;User Id=sa;Password=xxxxxxxxxx" 
        providerName="System.Data.SqlClient" />
</connectionStrings>

每当我尝试将迁移添加为时,我都会收到错误消息:

System.Data.Entity.Core.ProviderIncompatibleException:从数据库获取提供程序信息时出错。这可能是由 Entity Framework 使用不正确的连接字符串引起的。检查内部异常以获取详细信息并确保连接字符串正确。 ---> System.Data.Entity.Core.ProviderIncompatibleException:提供者未返回 ProviderManifestToken 字符串。 --->

System.Data.SqlClient.SqlException:建立与 SQL Server 的连接时发生与网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称是否正确以及 SQL Server 是否配置为允许远程连接。 (提供者:SQL 网络接口,错误:26 - 错误定位服务器/指定的实例)

【问题讨论】:

  • 你不应该使用base("name=ShoppingCartDb")而不是base("ShoppingCartDb")吗?

标签: sql-server-2012 entity-framework-6


【解决方案1】:

试试这个。确保您的 ShoppingCartContext 所在的项目是启动项目,或者在执行 add-migration 命令时包含参数 -startupprojectname ex。 add-migration -startupprojectname yourprojectname

【讨论】:

  • 将 WebApi 项目设置为“启动项目”是我的问题的解决方案。好收获!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多