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

时间:2015-11-30 10:32:14

标签: c# .net entity-framework firebird2.5

我尝试通过代码优先连接到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; }
}

和SoftwareContext。

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

    public SoftwareContext()
    {  }
}

当我运行它时似乎一切正常,直到我创建一个Software对象并将其添加到集合中。它抛出以下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类的构造函数中检查现有的DbProviderFactories时,该列表仅显示其他提供者。

  1. System.Data.Odbc
  2. System.Data.OleDb
  3. System.Data.OracleClient的
  4. System.Data.SqlClient的
  5. System.Data.SqlServerCe.4.0
  6. 当我让我给出Database.DefaultConnectionFactory时,它总是向我显示System.Data.Entity.Infrastructure.SqlConnectionFactory,而不是来自app.config的给定默认值。

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

    我找到了一些关于EF和Firebird无法迁移的主题,但是他们主要遇到其他问题。

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

1 个答案:

答案 0 :(得分:2)

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

在所有其他方面,它看起来像你的configuration is correct