簡體   English   中英

即使已更改,實體框架仍嘗試連接到舊的ADO.NET提供程序

[英]Entity Framework keeps trying to connect to old ADO.NET provider eventhough it has been changed

我將MySQL項目的數據庫連接更改為從MySQL定位到SQL Server。 但是,當嘗試運行針對SQL Server中的數據庫的Update-Database -StartProjectName ProjectName ,出現以下錯誤

指定的架構無效。 錯誤:(0,0):錯誤0152:未找到具有不變名稱'MySql.Data.MySqlClient'的ADO.NET提供程序的實體框架提供程序。 確保提供程序已在應用程序配置文件的“ entityFramework”部分中注冊。 有關更多信息,請參見http://go.microsoft.com/fwlink/?LinkId=260882

所以我試圖確保我的項目沒有MySQL

  1. 使用ctrl + F並搜索所有MySQL關鍵字:找不到任何關鍵字。
  2. 通過NuGET軟件包管理器卸載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; }
    }
}

所以對我DbConfiguration我創建了自己的DbConfiguration類,並使用App.config的codeConfigurationType屬性對其進行了引用

我的自定義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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM