我在我的应用程序中使用这个InventoryMgmtContext
并且我多年没有触及它。但我现在正试图在新的测试项目中使用它并解决这个问题。常规应用程序仍然正常运行,问题就在于尝试运行此测试时。所有这些都通过编译。在测试执行期间,在运行时抛出错误。
我确实看到了this similar question,但没有一个答案适用或适用于我。
请注意,所有这些涉及的项目都在同一个解决方案中。以下是我尝试过的一些事情。
还不确定还有什么
错误:
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.dll
和InciteCore.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();
我遇到了同样的问题,因为我使用IDbContext接口而没有实现这个方法DbSet<T> Set<T>() where T : class;
但是当我在我的Context基类中实现它时问题解决了我认为你需要在这个部分类中实现IDbContext方法InventoryMgmtContext