TypeLoadException方法“设置”没有实现

时间:2019-02-13 17:11:40

标签: c# dll typeloadexception

我在应用程序的所有位置都使用了InventoryMgmtContext,多年来我都没有碰过它。但是我现在正尝试在新的Test项目中使用它并解决此问题。常规应用程序仍然可以正常运行,问题出在尝试运行此测试时。所有这些都通过编译。在测试执行过程中,该错误在运行时引发。

我确实看到了this similar question,但是没有一个答案对我有用或有用。

请注意,所有这些涉及的项目都在同一解决方案中。这是我尝试过的一些事情。

  • 清洁和重建项目。
  • 手动删除我的Test项目的bin文件夹中的文件
  • 确保测试项目中引用的OTIS.Domain.dll版本是最新版本,该版本是在解决方案构建期间创建的。

不知道还有什么

错误:

Message: Test method ShopifyAdapterUnitTests.ManageProductTests.GetAndImportUpdatedProductsProducts threw exception: System.TypeLoadException: Method 'Set' in type 'OTIS.Domain.InventoryMgmt.InventoryMgmtContext' from assembly 'OTIS.Domain, Version=1.5.6983.16416, Culture=neutral, PublicKeyToken=null' does not have an implementation.

我的IDbContext界面:

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

namespace InciteCore.Domain
{
    public interface IDbContext
    {
        DbSet<T> Set<T>() where T : class;
        DbEntityEntry<T> Entry<T>(T entity) where T : class;
        int SaveChanges();
        void Dispose();
    }    
}

由Entity Framework DB First创建的部分类InventoryMgmtContext,它继承自System.Data.Entity.DbContext,该子类具有Set方法:

namespace OTIS.Domain.InventoryMgmt
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Core.Objects;
    using System.Data.Entity.Infrastructure;

    public partial class InventoryMgmtContext : DbContext
    {
        public InventoryMgmtContext()
            : base("name=InventoryMgmtContext")
        {
        }
        <...>
     }
}

我创建了自己的部分类声明以扩展EF,以确保它符合IDbContext接口,该接口指定了Set方法。 使用InciteCore.Domain; 使用System.Data.Entity;

namespace OTIS.Domain.InventoryMgmt
{
    public partial class InventoryMgmtContext : DbContext, IDbContext
    {       

    }
}

我的测试方法,实例化一个新的InventoryMgmtContext,这是引发错误的地方。注意,我还包括对它的Set方法的调用!!!那我为什么会收到这个错误?该项目确实同时引用了OTIS.Domain.dllInciteCore.Domain

public async Task GetAndImportUpdatedProductsProducts()
{
    InventoryMgmtContext dbContext = new InventoryMgmtContext();
    var items = dbContext.Set<Item>(); <---- Set Method call!!!
    var repository = new InciteCore.Data.Repository<StoreFront>(dbContext);
    var storeFront = await repository.SearchFor(s => s.Id == 8).FirstOrDefaultAsync();

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,因为我使用IDbContext接口而不实现此方法DbSet<T> Set<T>() where T : class;,但是在您的情况下在我的Context基类中实现它时,问题解决了,我认为您需要在此局部类中实现IDbContext方法InventoryMgmtContext

相关问题