使用DI工具unity创建EF ObjectContext对象
我尝试使用下面的脚本在容器中注册类型,它运行良好
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
找到原因,与CLR无法正确加载程序集System.Data.Entity有关。应添加 publicktokenkey 以及文化和版本。以下配置有效:
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:
你的配置是错误的。应该是:
Your configuration is wrong. It should be: