I'm seeing an issue while attempting to resolve my service class thatDbContext since it is an abstract class, it cannot be built. Here is the error message:

Unity.Exceptions.ResolutionFailedException: 'Resolution of the dependency failed, type = 'MyService.MyClass', name = '(none)'.
Exception occurred while: while resolving.
Exception is: InvalidOperationException - The current type, System.Data.Common.DbConnection, is an abstract class and cannot be constructed. Are you missing a type mapping?
At the time of the exception, the container was: 
  Resolving ...
          Resolving MyApp.MyRepository,(none)
          Resolving parameter 'myDbContext' of constructor MyApp.MRepository(MyApp.IMyDbContext myDbContext)
              Resolving parameter 'existingConnection' of constructor MyApp.MyDbContext(System.Data.Common.DbConnection existingConnection, System.Data.Entity.Infrastructure.DbCompiledModel model, System.Boolean contextOwnsConnection)
                Resolving System.Data.Common.DbConnection,(none)

The DBContext appears as follows:

public class MyDbContext : System.Data.Entity.DbContext, IMyDbContext
    public MyDbContext()
        : base("MainConnectionString")

My best opinion is that EF searches the configuration file for a connection string, yet the connection string is defined:

    <add name="MainConnectionString" connectionString="Server=.\SQLEXPRESS;Database=MyDB;User Id= . . ." providerName="System.Data.SqlClient" />

Registration for Unity:

In light of the fact that I obviously have something wrong with the settings, my question is how EF determines how to generate this class.

UnityContainer container = new UnityContainer();
container.RegisterType<IMyDbContext, MyDbContext>();
container.RegisterType<IMyRepository, MyRepository>();
1/30/2018 1:28:37 PM

Accepted Answer

1/30/2018 1:30:17 PM

Popular Answer


Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow