使用DI工具统一来创建EF ObjectContext对象

时间:2011-03-16 03:09:08

标签: entity-framework-4 unity-container objectcontext

我尝试使用下面的脚本在容器中注册类型,它运行良好

 Container.RegisterType<System.Data.Objects.ObjectContext,
              ExSS.Repository.MyEntity>( "myentity", new InjectionConstructor());

然而,当我尝试使用xml config时:

<alias alias="ObjectContext" 
        type="System.Data.Objects.ObjectContext,System.Data.Entity" />
<alias alias="MyEntity" type="ExSS.Repository.MyEntity,ExSS.Repository"/>
<register type="ExSS.Repository.MyEntity,ExSS.Repository" mapTo="MyEntity" 
        name="myentity">
    <constructor></constructor>
</register>

它不起作用。错误消息是:

The type name or alias ObjectContext could not be resolved. Please check your 
configuration file and verify this type name. 

描述:

An unhandled exception occurred during the execution of the current web
request. Please review the stack trace for more information about the error 
and where it originated in the code. 

异常详细信息:

System.InvalidOperationException: The type name or alias ObjectContext could 
not be resolved. Please check your configuration file and verify this type name.

堆栈跟踪是:

[InvalidOperationException: The type name or alias ObjectContext could not be resolved. Please check your configuration file and verify this type name.]
   Microsoft.Practices.Unity.Configuration.ConfigurationHelpers.TypeResolverImpl.ResolveType(String typeNameOrAlias, Boolean throwIfResolveFails) +200
   Microsoft.Practices.Unity.Configuration.ConfigurationHelpers.TypeResolver.ResolveType(String typeNameOrAlias) +59
   Microsoft.Practices.Unity.Configuration.RegisterElement.GetRegisteringType() +70
   Microsoft.Practices.Unity.Configuration.RegisterElement.ConfigureContainer(IUnityContainer container) +111
   Microsoft.Practices.Unity.Configuration.ContainerConfiguringElement.ConfigureContainerInternal(IUnityContainer container) +39
   Microsoft.Practices.Unity.Configuration.<>c__DisplayClass1.<ConfigureContainer>b__0(ContainerConfiguringElement element) +42
   Microsoft.Practices.ObjectBuilder2.EnumerableExtensions.ForEach(IEnumerable`1 sequence, Action`1 action) +200
   Microsoft.Practices.Unity.Configuration.ContainerElement.ConfigureContainer(IUnityContainer container) +269
   Microsoft.Practices.Unity.Configuration.UnityConfigurationSection.Configure(IUnityContainer container, String configuredContainerName) +133
   Microsoft.Practices.Unity.Configuration.UnityContainerExtensions.LoadConfiguration(IUnityContainer container, UnityConfigurationSection section, String containerName) +70

任何人都可以给出一些建议吗?非常感谢

2 个答案:

答案 0 :(得分:1)

找到原因,它与该程序集有关.CRL正确无法加载System.Data.Entity。应添加publicktokenkey和文化和版本。下面的配置工作:

<alias alias="ObjectContext" type="System.Data.Objects.ObjectContext, System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> 
<alias alias="MyEntity" type="ExSS.Repository.MyEntity, ExSS.Repository"/> 
<register type="ObjectContext" mapTo="MyEntity" name="myentity"> 
  <constructor /> 
</register>

答案 1 :(得分:0)

您的配置错误。它应该是:

<alias alias="ObjectContext" type="System.Data.Objects.ObjectContext, System.Data.Entity" />
<alias alias="MyEntity" type="ExSS.Repository.MyEntity, ExSS.Repository"/>
<register type="ObjectContext" mapTo="MyEntity" name="myentity">
    <constructor />
</register>