为什么EF 6无法识别我的Firebird数据库提供商

本文关键字:我的 Firebird 数据库 提供商 识别 EF 为什么 | 更新日期: 2023-09-27 18:25:05

我尝试通过代码第一次尝试连接到EF 6中的新Firebird数据库。在我尝试插入某些内容之前,系统不会创建数据库,也找不到我在app.config中提供的数据库提供程序。

我的项目生活在一个图书馆项目中,所有EF的东西都在这里完成,还有一个控制台应用程序,显示我返回的数据。

我在项目中安装了以下NuGet软件包。

  • EntityFramework 6.1.3
  • EntityFramework.Firebird.4.8.1
  • FirebirdSql.Data.FirebirdClient.4.8.1.1

我的配置很少遵循约定而非配置方法。

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
        <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    </configSections>
    <system.data>
        <DbProviderFactories>
            <remove invariant="FirebirdSql.Data.FirebirdClient" />
            <add name="FirebirdClient Data Provider" invariant="FirebirdSql.Data.FirebirdClient" description=".NET Framework Data Provider for Firebird" type="FirebirdSql.Data.FirebirdClient.FirebirdClientFactory, FirebirdSql.Data.FirebirdClient" />
        </DbProviderFactories>
    </system.data>
    <entityFramework>
        <defaultConnectionFactory type="FirebirdSql.Data.EntityFramework6.FbConnectionFactory, EntityFramework.Firebird" />
        <providers>
            <provider invariantName="FirebirdSql.Data.FirebirdClient" type="FirebirdSql.Data.EntityFramework6.FbProviderServices, EntityFramework.Firebird" />
            <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
        </providers>
    </entityFramework>
    <connectionStrings>
        <add name="SoftwareContext" connectionString="database=localhost:Inventory;user=user;password=password;ServerType=0;" providerName="FirebirdSql.Data.FirebirdClient" />
    </connectionStrings>
    <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
            <dependentAssembly>
                <assemblyIdentity name="FirebirdSql.Data.FirebirdClient" publicKeyToken="3750abcc3150b00c" culture="neutral" />
                <bindingRedirect oldVersion="0.0.0.0-4.5.0.0" newVersion="4.5.0.0" />
            </dependentAssembly>
        </assemblyBinding>
    </runtime>
</configuration>

该模型由一个没有注释的简单POCO实现组成。

public class Software
{
    public int Id { get; set; }
    public string Name { get; set; }
    public Version VersionNumber { get; set; }
}

以及软件上下文。

public class SoftwareContext : DbContext
{
    public DbSet<Software> Programs { get; set; }
    public SoftwareContext()
    {  }
}

当我运行它时,一切似乎都很好,直到我创建了一个软件对象并将其添加到集合中。它向我抛出以下System.NotSupportedException:

No Entity Framework provider found for the ADO.NET provider with invariant name 'System.Data.SqlClient'

当我明确地将SoftwareContext的构造函数更改为:时

public SoftwareContext() : base(new FbConnection(constring), true)

当我尝试相同的操作时,会引发以下System.NotSupportedException:

Unable to determine the provider name for provider factory of type 'FirebirdSql.Data.FirebirdClient.FirebirdClientFactory'. Make sure that the ADO.NET provider is installed or registered in the application config.

当我在SoftwareContext类的构造函数中检查现有的DbProviderFactory时,列表只显示其他提供者。

  1. System.Data.Odbc
  2. System.Data.OleDb
  3. System.Data.OracleClient
  4. System.Data.SqlClient
  5. System.Data.SqlServerCe.4.0

当我给Database.DefaultConnectionFactory时,它总是显示System.Data.Entity.Infrastructure.SqlConnectionFactory,而不是app.config中的给定默认值。

EF和Firebird的所有DLL在输出中都设置为本地副本。因此,它们在运行时都可用。该数据库是一个标准的2.5.5安装,没有嵌入式版本。

我发现了一些关于EF和Firebird无法正常迁移的话题,但他们大多遇到了其他问题。

为什么EF不识别配置中的数据库提供程序,因此不创建数据库?

为什么EF 6无法识别我的Firebird数据库提供商

您的问题是.NET使用的配置不是库的配置文件中指定的,而是主项目中指定的。也就是说,如果你的应用程序是一个网站,你需要在web.config中指定你的配置。如果是控制台或桌面应用程序,则需要将您的配置添加到该项目的app.config中。而且,如果您想运行集成测试,您需要将配置添加到测试项目中。

在所有其他方面,您的配置看起来都是正确的。