2

I am using this InventoryMgmtContext all over the place in my application and I haven't touched it for years. But I am now trying to use it in a new Test project and getting this issue. The regular application still runs fine, the issue is just when trying to run this test. And all this passes compilation. The error is thrown at run time during test execution.

I did see this similar question, but none of the answers applied or worked for me.

Note all of these involved projects are all in the same solution. Here are a few things I've tried.

  • Cleaning and Rebuilding the project.
  • Manually deleting files in the the bin folder of my Test project
  • Ensuring the referenced OTIS.Domain.dll version in the test project is the latest, that was created during the solution build.

Not sure what else

Error:

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.

My IDbContext interface:

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();
    }    
}

The partial class InventoryMgmtContext created by Entity Framework DB First, which inherits from System.Data.Entity.DbContext, which has a Set method:

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")
        {
        }
        <...>
     }
}

I created my own partial class declaration to augment EFs to make sure it conforms to the IDbContext interface, which specifies the Set method. using InciteCore.Domain; using System.Data.Entity;

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

    }
}

My test method, instantiating a new InventoryMgmtContext, which is where the error is thrown. Note I also include a call to it's Set method!!! So why could I be getting this error? This project does have a reference to both OTIS.Domain.dll and 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();
crichavin
  • 4,672
  • 10
  • 50
  • 95
  • The database model may of changed and you need to refresh : See 5. Dealing with model changes : https://learn.microsoft.com/en-us/ef/ef6/modeling/designer/workflows/model-first – jdweng Feb 13 '19 at 17:27
  • The only thing a version number like 1.5.6983.16416 ever does is practically guaranteeing that you'll use the wrong version. Delete any bindingRedirect you might have used to paper over a version number problem, use Fuslogvw.exe to verify you get the DLL you are expecting. – Hans Passant Feb 13 '19 at 17:36
  • @jdweng - I used the Update Model from Database feature to ensure it was in sync. Still the same issue, but thanks for an idea to try. – crichavin Feb 13 '19 at 17:48
  • @HansPassant I don't have any bindingRedirects in the app.config file in this project for any of the internal project dlls like OTIS.Domain.dll...only for third party dlls. I ran fuslogvw to view the bindings while running the test project. No errors existed and the correct dll versions were referenced. – crichavin Feb 13 '19 at 18:14
  • I think the version of does not have an implementation has changed. Open the .proj file and see the version that is used. I sometimes get type issue fixed by editing the proj file and changing version. Normally I create a new project with just the object that is causing an issue. Then build and open the new .proj file and compare version with bad project. The compiler dependencies are not very good and updating a project often fails. You could start a new project from scratch and add all the modules which usually works. – jdweng Feb 13 '19 at 19:20
  • Hi @ChadRichardson, I'm not able to reproduce. I realize this is a rather old question, but if you're still interested, could you please clarify a little more? Where does the `GetAndImportUpdatedProductsProducts()` method belong, and why is it truncated? There's also some code improperly formatted as such in the question. I trust you're aware of [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). Let me know if you edit so that I can continue working towards a solution. – Marc.2377 Mar 01 '19 at 20:00

1 Answers1

0

I faced same problem because I use IDbContext interface without implement this methodDbSet<T> Set<T>() where T : class; but the problem solved when I implement it in my Context base class in your case I think you need to Implement the IDbContext Methods in this partial class InventoryMgmtContext

  • 1
    Thanks Ahmad. But since InventoryMgmtContext inherits DBContext, which already has those methods, shouldn't that suffice? – crichavin Feb 20 '19 at 21:26
  • I think it's not suffice I assume you should add implement in `InventoryMgmtContext : DbContext, IDbContext` to work, in my case I used DBcontextBase rather than partial class and implement the method `Set` in there – Ahmad Mostafa Awaly Feb 21 '19 at 13:34
  • @ChadRichardson yes, in fact it should; I tested locally and did not get a TypeLoadException at runtime for that reason precisely. Perhaps, in your new test project, you forgot to inherit from `DBContext` in one of the partial class' definitions, or their names / namespaces did not match? – Marc.2377 Mar 01 '19 at 20:04
  • @Marc.2377 - well the partial class version that EntityFramework creates in this Database First implementation does not inherit from DBContext, which is why I created another partial class which you can see in my code snippet, so that it does inherit from DBContext. – crichavin Mar 02 '19 at 00:19
  • @ChadRichardson right; but perhaps, *in your new test project* you forgot to do that, a mistake which is not replicated in your question? – Marc.2377 Mar 02 '19 at 00:26
  • @Marc.2377 thanks for throwing out ideas, I appreciate your time in doing so. The Test project is actually Unit Test project, and that references the project that InventoryMgmtContext lives in, it doesn't have it's own InventoryMgmtContext class. – crichavin Mar 02 '19 at 00:28
  • @ChadRichardson No problem. I see; I'll do some more testing of my own later, to try and reproduce the issue. – Marc.2377 Mar 02 '19 at 00:32
  • 4
    Hi @ChadRichardson I had the same problem as you have. The error in my solution was caused by different versions of the EntityFramework nuget package. The problem was gone after I fixed that. – Marcel Beeker May 31 '19 at 13:44
  • You should make that as an answer @MarcelBeeker, it helps me fix my error. – Rosdi Kasim Mar 09 '22 at 22:07