实体框架一直在尝试连接到旧的ADO.NET提供程序,尽管它已被更改

时间:2017-09-27 07:38:18

标签: c# mysql sql-server entity-framework

我将MySQL项目的数据库连接从MySQL更改为目标SQL Server。但是,当尝试在SQL Server中运行Update-Database -StartProjectName ProjectName目标数据库时,我收到以下错误

  

指定的架构无效。错误:(0,0):错误0152:没有实体   为具有不变名称的ADO.NET提供程序找到的框架提供程序   'MySql.Data.MySqlClient'。确保提供商已注册   应用程序配置文件的“entityFramework”部分。看到   http://go.microsoft.com/fwlink/?LinkId=260882了解更多信息。

所以我试图确保我的项目没有MySQL

  1. 使用ctrl + F并搜索所有MySQL关键字:找不到。
  2. 通过NuGET包管理器
  3. 卸载MySQL包

    为什么当我明确表示应该在配置文件中使用默认的MS SQL提供程序时,Entity Framework会不断向我询问MySQL提供程序?

    这是我的配置文件:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <configSections>
        <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
        <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
      </configSections>
      <connectionStrings>
       <add name="Connection" connectionString="Data Source=SQLSERVER123;Database=DB123;Integrated Security=true" providerName="System.Data.SqlClient" />
      </connectionStrings>
      <entityFramework>
        <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
          <parameters>
            <parameter value="mssqllocaldb" />
          </parameters>
        </defaultConnectionFactory>
        <providers>
          <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
        </providers>
      </entityFramework>
    </configuration>
    

    这是我的DbContext文件:

    using System;
    using System.Data.Entity;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Linq;
    
    namespace ProjectName.MSSQL
    {
        public class ProjectNameContext:DbContext
        {
            public ProjectNameContext()
                : base("Connection")
            {
    
            }
    
            public static ProjectNameContext Create()
            {
                return new ProjectNameContext();
            }
    
            public DbSet<Users> Users { get; set; }
        }
    }
    

1 个答案:

答案 0 :(得分:0)

那对我有用的是I created my own DbConfiguration class and referenced it using the codeConfigurationType property in App.config

我的自定义Dbconfiguration课程

using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.SqlServer;

namespace ProjectName.MSSQL
{
    public class MSSQLDbConfiguration:DbConfiguration
    {
        public MSSQLDbConfiguration()
        {
            this.SetProviderServices("System.Data.SqlClient", System.Data.Entity.SqlServer.SqlProviderServices.Instance);
            this.SetDefaultConnectionFactory(new LocalDbConnectionFactory("v11.0"));
        }
    }
}

我的App.config

<entityFramework codeConfigurationType="ProjectName.MSSQL.MSSQLDbConfiguration, ProjectName.MSSQL">
...
any stuff...
...
</entityFramework>