构造函数使用Unity/Prism/MVVM注入ObjectContext

Kai*_*pka 3 c# dependency-injection unity-container mvvm entity-framework-4

我使用带有MVVM模式和Prism的WPF开发了一个应用程序.视图将添加到ModuleCatalog中,并且视图模型将注册到统一容器.为此,我正在使用一个负责创建shell的Bootstrapper,配置统一容器和模块目录.
现在的问题是,如何将我的EntityContext注入到几个视图模型中.
首先是Bootstrapper:

 
public class Bootstrapper : UnityBootstrapper
    {
        protected override DependencyObject CreateShell()
        {
            Shell shell = Container.Resolve();
            shell.Show();
            return shell;
        }

    protected override void ConfigureContainer()
    {
        base.ConfigureContainer();
        Container.RegisterType<EntityContext >("Context");
        Container.RegisterType<PersonViewModel>(new InjectionConstructor(
            new ResolvedParameter<EntityContext >("Context")));
    }

    protected override IModuleCatalog GetModuleCatalog()
    {
        ModuleCatalog catalog = new ModuleCatalog();
        catalog.AddModule(typeof(PersonModule));
        return catalog;
    }
Run Code Online (Sandbox Code Playgroud)

viewmodel看起来像那样(摘录)


public class PersonViewModel : ViewModelBase, IDataErrorInfo
    {
        private Person _person;
        private PersonRepository _repository;
        readonly EntityContext _context;

    public PersonViewModel(EntityContext context)
    {
        _context = context;
        _person = new Person();
        _repository = new PersonRepository(context);
    }
Run Code Online (Sandbox Code Playgroud)

模块:


    public class PersonModule : IModule
    {
        private readonly IRegionManager regionManager;

    public PersonModule(IRegionManager regionManager)
    {
        this.regionManager = regionManager;
    }

    public void Initialize()
    {
        regionManager.RegisterViewWithRegion("PersonData", typeof(PersonView));
    }

}
Run Code Online (Sandbox Code Playgroud)

代码隐藏视图:


    public partial class PersonView : UserControl
    {
        private PersonViewModel _vm;

    public PersonView()
    {
        InitializeComponent();
    }

    [Dependency]
    public PersonViewModel VM
    {
        get
        {
            return this.DataContext as PersonViewModel;
        }
        set
        {
            _vm = value;
            this.DataContext = _vm;
        }
    }      
}
Run Code Online (Sandbox Code Playgroud)

我不确定我的方法原则上是否正常工作,但为了保存对数据库的更改,我需要知道所做的更改.现在它显然不起作用,因为发生了ModuleInitializeException.Stacktrace:
初始化模块'PersonModule'时发生异常.
- 异常消息是:尝试将视图添加到区域"PersonData"时发生异常.
- 最可能导致异常的原因是:'System.InvalidOperationException:类型EntityContext有多个长度为1的构造函数.无法消除歧义.
bei Microsoft.Practices.ObjectBuilder2.ConstructorSelectorPolicyBase 1.SelectConstructor(IBuilderContext context)1.FindLongestConstructor(Type typeToConstruct)
bei Microsoft.Practices.ObjectBuilder2.ConstructorSelectorPolicyBase

bei Microsoft.Practices.ObjectBuilder2.DynamicMethodConstructorStrategy.PreBuildUp(IBuilderContext context)
bei Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context)'.
但是还要检查InnerExceptions以获取更多详细信息,或者调用.GetRootException().- 尝试加载模块的程序集是:App,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null
检查异常的InnerException属性以获取更多信息.如果在DI容器中创建对象时发生异常,则可以使用exception.GetRootException()来帮助找到问题的根本原因.

如果对于这个问题有其他解决方案,我对它持开放态度,但我想要使用或多或少的基本结构.
提前致谢.