0

I'm trying to inject an EF ObjectContext using Unity constructor injection. My attemp is to register the type in the bootsprapper like this:

protected override void ConfigureContainer()
{
    base.ConfigureContainer();
    Container.RegisterType<ObjectContext, MyObjectContext>(new InjectionConstructor());
}

EF creates mutiple constructors which look like these

public MyObjectContext() : base("name=MyObjectContext", "MyObjectContext")
public MyObjectContext(string connectionString) : base(connectionString, "MyObjectContext")
public MyObjectContext(EntityConnection connection) : base(connection, "MyObjectContext")

When debugging my code Unity throws a ResolutionFailedException telling me "The type MyObjectContext has multiple constructors of length 1. Unable to disambiguate." at the time when a new class that has the following constructor is resolved.

public MainViewModel(UnityContainer container, MyObjectContext entities)

As far as I know using RegisterType with new InjectionConstructor() as argument ensures that the default parameterless constructor is called (thats what I want). Why can't Unity resolve the type as expected? Do I miss anything?

Best Regards

Jay

1 Answer 1

3

Your registration looks right. You sure the registration code is getting called? Try putting a breakpoint on it.

(Off topic: why are you passing your container to your viewmodel?)

Sign up to request clarification or add additional context in comments.

1 Comment

I mark this as an answer because it led me to the solution. It seems that Unity had some issues resolving the type because I had registered it at the wrong place. I passed the container because I thought I would need it to resolve registered instances/types across projects (shell, modules, infrastructure etc.) but that doesn't seem to be the right/working way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.