Effort + Autofac : 没有 return 继承自 DbProviderServices 的对象

Effort + Autofac : did not return an object that inherits from DbProviderServices

我正在使用 EffortEffort.EF6 作为我的内存数据库来测试我的 Web API 2 应用程序。

我正在使用 Autofac 进行 DI 解析。

这是我在 Startup.cs

中的代码
builder.Register(c =>
                {
                    var dbConnectionFactory =
                        Effort.DbConnectionFactory.CreatePersistent(nameof(CvManagementDbContext));
                    return new CvManagementDbContext(dbConnectionFactory);
                })
                .As<DbContext>()
                .SingleInstance();

在我的 CvManagementDbContext.cs 中:

public CvManagementDbContext(DbConnection connection)
            : base(connection, true)
        {
        }

[更新]

这是我的package.config

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="ApiMultipartFormDataFormatter" version="1.0.3" targetFramework="net452" />
  <package id="Autofac" version="4.8.1" targetFramework="net46" />
  <package id="Autofac.WebApi2" version="4.2.0" targetFramework="net46" />
  <package id="AutoMapper" version="7.0.1" targetFramework="net46" />
  <package id="AutoMapper.EF6" version="1.1.1" targetFramework="net46" />
  <package id="DelegateDecompiler" version="0.24.0" targetFramework="net46" />
  <package id="DelegateDecompiler.EntityFramework" version="0.24.0" targetFramework="net46" />
  <package id="Effort" version="1.3.10" targetFramework="net46" />
  <package id="Effort.EF6" version="1.3.10" targetFramework="net46" />
  <package id="EntityFramework" version="6.2.0" targetFramework="net46" />
  <package id="JWT" version="4.0.0" targetFramework="net46" />
  <package id="Microsoft.AspNet.Cors" version="5.2.6" targetFramework="net46" />
  <package id="Microsoft.AspNet.WebApi" version="5.2.6" targetFramework="net46" />
  <package id="Microsoft.AspNet.WebApi.Client" version="5.2.6" targetFramework="net46" />
  <package id="Microsoft.AspNet.WebApi.Core" version="5.2.6" targetFramework="net46" />
  <package id="Microsoft.AspNet.WebApi.Cors" version="5.2.6" targetFramework="net46" />
  <package id="Microsoft.AspNet.WebApi.Owin" version="5.2.6" targetFramework="net46" />
  <package id="Microsoft.AspNet.WebApi.WebHost" version="5.2.6" targetFramework="net46" />
  <package id="Microsoft.CodeDom.Providers.DotNetCompilerPlatform" version="2.0.1" targetFramework="net46" />
  <package id="Microsoft.Net.Compilers" version="2.9.0" targetFramework="net46" developmentDependency="true" />
  <package id="Microsoft.Owin" version="4.0.0" targetFramework="net46" />
  <package id="Microsoft.Owin.Host.SystemWeb" version="4.0.0" targetFramework="net46" />
  <package id="Mono.Reflection" version="1.1.0.0" targetFramework="net46" />
  <package id="Mono.Reflection.Core" version="1.1.1" targetFramework="net46" />
  <package id="Newtonsoft.Json" version="11.0.2" targetFramework="net46" />
  <package id="NMemory" version="2.0.4" targetFramework="net46" />
  <package id="Owin" version="1.0" targetFramework="net46" />
  <package id="ServiceStack.Common" version="5.4.0" targetFramework="net46" />
  <package id="ServiceStack.Interfaces" version="5.4.0" targetFramework="net46" />
  <package id="ServiceStack.Redis" version="5.4.0" targetFramework="net46" />
  <package id="ServiceStack.Text" version="5.4.0" targetFramework="net46" />
  <package id="System.Buffers" version="4.5.0" targetFramework="net46" />
  <package id="System.Memory" version="4.5.1" targetFramework="net46" />
  <package id="System.Runtime.CompilerServices.Unsafe" version="4.5.1" targetFramework="net46" />
  <package id="System.ValueTuple" version="4.5.0" targetFramework="net46" />
</packages>

Web.config

中的Effort配置
<entityFramework>
    <interceptors>
      <interceptor type="Cv_Management.Interceptor.GlobalDbCommandInterceptor, Cv_Management" />
    </interceptors>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <!--<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />-->
      <provider invariantName="Effort.Provider" type="Effort.Provider.EffortProviderServices,Effort" />
    </providers>
  </entityFramework>

当我的应用程序启动时在控制台中抛出异常,即:

'The 'Instance' member of the Entity Framework provider type 'Effort.Provider.EffortProviderServices, Effort, Version=1.0.0.0, Culture=neutral, PublicKeyToken=6a46696d54971e6d' did not return an object that inherits from 'System.Data.Entity.Core.Common.DbProviderServices'. Entity Framework providers must inherit from this class and the 'Instance' member must return the singleton instance of the provider. This may be because the provider does not support Entity Framework 6 or later; see http://go.microsoft.com/fwlink/?LinkId=260882 for more information.'

我做错了什么?

我想,我成功地重现了你的问题。你看EffortEffort.EF6是互斥的库,你只是其中之一。根据努力 github:

If you use Entity Framework 6...

  • You will need the Effort.EF6 package
  • Check the offical NuGet site

otherwise...

  • You will need the Effort package
  • Check the offical NuGet site

当我从我的 csproj 文件中的 Effort 包中引用 dll 时,我得到了你的错误:

<Reference Include="Effort, Version=1.0.0.0, Culture=neutral, PublicKeyToken=6a46696d54971e6d, processorArchitecture=MSIL">
  <SpecificVersion>False</SpecificVersion>
  <HintPath>..\packages\Effort.1.3.10\lib\net45\Effort.dll</HintPath>
</Reference>

当我从 Effort.EF6 包更改对 dll 的引用时,错误消失了:

<Reference Include="Effort, Version=1.0.0.0, Culture=neutral, PublicKeyToken=6a46696d54971e6d, processorArchitecture=MSIL">
  <SpecificVersion>False</SpecificVersion>
  <HintPath>..\packages\Effort.EF6.1.3.10\lib\net45\Effort.dll</HintPath>
</Reference>

检查您的 csproj 文件中的引用,确保您使用的是来自 Effort.EF6 的 dll。并摆脱 Effort 包。希望对你有帮助。