1

I'm using GenericRepository pattern from https://github.com/huyrua/efprs. I just want to select constructor with DbContext as parameter. I know there's duplicate question, but solution from this didn't solve. Here's my configuration:

ObjectFactory.Initialize(x =>
{
    x.Scan(scan =>
    {
        scan.TheCallingAssembly();
        scan.AssemblyContainingType<Data.Entity.TokoContainer>();
        scan.WithDefaultConventions();
    });
    x.For<DbContext>().Use<Data.Entity.TokoContainer>();
    x.For<Infrastructure.Data.IRepository>()
     .Use<Infrastructure.Data.GenericRepository>()
     .Ctor<DbContext>().Is(c => c.GetInstance<DbContext>());
});

this cause error "StructureMap Exception Code: There is no argument of type System.Data.Entity.DbContext for concrete type Infrastructure.Data.GenericRepository".

When using this:

x.SelectConstructor<Infrastructure.Data.IRepository>(() => new Infrastructure.Data.GenericRepository((DbContext)null));
x.ForConcreteType<Infrastructure.Data.IRepository>()
 .Configure.Ctor<DbContext>().Is(c => c.GetInstance<DbContext>());

Causing "StructureMap configuration failures: Error 104".

Specifying from first code, adding parameter name "context" like this:

x.For<Infrastructure.Data.IRepository>()
 .Use<Infrastructure.Data.GenericRepository>()
 .Ctor<DbContext>("context").Is(c => c.GetInstance<DbContext>());

causing error "Missing requested Instance property "connectionStringName" for InstanceKey xxx". I don't know what to do now.

Any solution would be appreciated.

Community
  • 1
  • 1
iroel
  • 1,760
  • 1
  • 18
  • 27
  • "There is no argument of type System.Data.Entity.DbContext for concrete type Infrastructure.Data.GenericRepository". That's because the `GenericRepository` depends on `ObjectContext`. Not `DbContext`. – Steven May 27 '13 at 21:08
  • I am using the same library and having same exception. no wonder – Deeptechtons Jun 30 '16 at 09:50

1 Answers1

0

The StructureMap is trying to express, that the DbContext has constructor with a string parameter "connectionStringName". As you can see in the test scenario of the link you've appended: MyDbContext.cs

public class MyDbContext : DbContext
{
   ...    
   public MyDbContext(string connStringName) :
      base(connStringName)
   {
     ...

So, what we need to do, is correctly map the constructor of the DbContext. For example:

x.For<DbContext>()
 .Use<Data.Entity.TokoContainer>()
 // example, taking the first conn string - adjust as needed
 .Ctor<string>().Is(ConfigurationManager.ConnectionStrings[0].ConnectionString);
;

Now, even the DbContext would be correctly set

Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
  • Error: StructureMap Exception Code: 302. There is no argument of type System.String for concrete type Toko.Data.Entity.TokoContainer – iroel May 27 '13 at 11:03
  • If TokoContainer is your class, change its constructor. You have to pass the conn string to the base DBContext anyway – Radim Köhler May 27 '13 at 11:07