开发者

Use DI tool unity to create EF ObjectContext object

I tried to register type in container using script below, it works well

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

however, when i try to use 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>

It does not work. The error message is:

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

Description:

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开发者_C百科 in the code. 

Exception Details:

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

The stack trace is:

[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

could anyone give some suggections? thanks very much


Found the reason, it is related to that assembly System.Data.Entity could not be loaded by CLR correctly. publicktokenkey and culture and version should be added. Config below works:

<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>


Your configuration is wrong. It should be:

<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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜