使用DI工具unity创建EF ObjectContext对象

发布于 2024-10-21 21:59:32 字数 2706 浏览 9 评论 0原文

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

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

,但是,当我尝试使用 xml 配置时:

<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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

缱倦旧时光 2024-10-28 21:59:32

找到原因,与CLR无法正确加载程序集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>

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>
别低头,皇冠会掉 2024-10-28 21:59:32

你的配置是错误的。应该是:

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

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文