TypeLoadException方法'Set'没有实现

问题描述 投票:0回答:1

我在我的应用程序中使用这个InventoryMgmtContext并且我多年没有触及它。但我现在正试图在新的测试项目中使用它并解决这个问题。常规应用程序仍然正常运行,问题就在于尝试运行此测试时。所有这些都通过编译。在测试执行期间,在运行时抛出错误。

我确实看到了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();
    }    
}

部分类InventoryMgmtContext由Entity Framework DB First创建,它继承自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();
c# dll typeloadexception
1个回答
0
投票

我遇到了同样的问题,因为我使用IDbContext接口而没有实现这个方法DbSet<T> Set<T>() where T : class;但是当我在我的Context基类中实现它时问题解决了我认为你需要在这个部分类中实现IDbContext方法InventoryMgmtContext

© www.soinside.com 2019 - 2024. All rights reserved.