EF 4.1 RC - “System.Data.Entity.IDatabaseInitializer`1[TContext]”违反了类型参数“TContext”的约束
我刚刚将我的项目(使用 NuGet)更新到 Entity Framework 4.1 RC 并收到此错误消息:
通用参数[0], 'Notesnhac.Library.NotesnhacContext', 在 'System.Data.Entity.IDatabaseInitializer`1[TContext]' 违反类型约束 参数“TContext”。
描述:未处理的异常 执行期间发生的 当前的网络请求。请查看 堆栈跟踪以获取有关的更多信息 错误及其起源 代码。
异常详细信息: 系统类型加载异常: 通用参数[0], 'Notesnhac.Library.NotesnhacContext', 在 'System.Data.Entity.IDatabaseInitializer`1[TContext]' 违反类型约束 参数“TContext”。
来源错误:
线路 114: DependencyResolver.SetResolver(新 StructureMapDependencyResolver(容器)); 第 115 行:#endregion 第 116 行:} 第 117 行:} 第 118 行:}
源文件:C:\projects\Kenny 项目\Notesnhac\Notesnhac.Site\Global.asax.cs 线路:116
堆栈跟踪:
[类型加载异常: 通用参数[0], 'Notesnhac.Library.NotesnhacContext', 在 'System.Data.Entity.IDatabaseInitializer`1[TContext]' 违反类型约束 参数“TContext”。]
笔记nhac.Site.MvcApplication.Application_Start() 在 C:\projects\肯尼 项目\Notesnhac\Notesnhac.Site\Global.asax.cs:116版本信息:Microsoft .NET 框架版本:4.0.30319;网络平台 版本:4.0.30319.225
它说错误在第 116 行,但我不认为这是错误所在。这是显示错误的代码片段,第 #116 行是 #endregion
之后的大括号:
protected void Application_Start()
{
// Initalizes the database
System.Data.Entity.Database.SetInitializer<NotesnhacContext>(new ContextInitializer());
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
AutoMapperConfig.CreateMappings();
ControllerBuilder.Current.DefaultNamespaces.Add("Notesnhac.Site.Controllers");
#region StructureMap IoC
IContainer container = new Container(x =>
{
x.For<IControllerActivator>().Use<StructureMapControllerActivator>();
x.Scan(s =>
{
s.Assembly("Notesnhac.Library");
s.TheCallingAssembly();
s.AddAllTypesOf<IController>().NameBy(type => type.Name.Replace("Controller", "").ToLower());
s.WithDefaultConventions();
});
});
DependencyResolver.SetResolver(new StructureMapDependencyResolver(container));
#endregion
}
谢谢。
I've just updated my project (using NuGet) to Entity Framework 4.1 RC and receives this error msg:
GenericArguments[0],
'Notesnhac.Library.NotesnhacContext',
on
'System.Data.Entity.IDatabaseInitializer`1[TContext]'
violates the constraint of type
parameter 'TContext'.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.TypeLoadException:
GenericArguments[0],
'Notesnhac.Library.NotesnhacContext',
on
'System.Data.Entity.IDatabaseInitializer`1[TContext]'
violates the constraint of type
parameter 'TContext'.Source Error:
Line
114: DependencyResolver.SetResolver(new
StructureMapDependencyResolver(container));
Line 115: #endregion Line 116: }
Line 117: } Line 118:}Source File: C:\projects\Kenny
Projects\Notesnhac\Notesnhac.Site\Global.asax.cs
Line: 116Stack Trace:
[TypeLoadException:
GenericArguments[0],
'Notesnhac.Library.NotesnhacContext',
on
'System.Data.Entity.IDatabaseInitializer`1[TContext]'
violates the constraint of type
parameter 'TContext'.]
Notesnhac.Site.MvcApplication.Application_Start()
in C:\projects\Kenny
Projects\Notesnhac\Notesnhac.Site\Global.asax.cs:116Version Information: Microsoft .NET
Framework Version:4.0.30319; ASP.NET
Version:4.0.30319.225
It says the error is on line 116 but I don't think it's where the error is. Here's a snip of the code where it says the error, line #116 is the curly brace right after #endregion
:
protected void Application_Start()
{
// Initalizes the database
System.Data.Entity.Database.SetInitializer<NotesnhacContext>(new ContextInitializer());
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
AutoMapperConfig.CreateMappings();
ControllerBuilder.Current.DefaultNamespaces.Add("Notesnhac.Site.Controllers");
#region StructureMap IoC
IContainer container = new Container(x =>
{
x.For<IControllerActivator>().Use<StructureMapControllerActivator>();
x.Scan(s =>
{
s.Assembly("Notesnhac.Library");
s.TheCallingAssembly();
s.AddAllTypesOf<IController>().NameBy(type => type.Name.Replace("Controller", "").ToLower());
s.WithDefaultConventions();
});
});
DependencyResolver.SetResolver(new StructureMapDependencyResolver(container));
#endregion
}
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题似乎出在这一行:
通用参数
TContext
是 需要是 DbContext 子类型。并且您的策略必须实现 IDatabaseInitializer。您没有显示
NotesnhacContext
的声明,但编译器表示缺少其中之一。您根本不需要指定类型参数;将从论证中推断出来。您可以这样做:
...假设您首先通过声明解决问题。
The problem seems to be the line:
The generic parameter
TContext
is required to be a DbContext subtype. And your strategy must implement IDatabaseInitializer.You don't show the declaration of
NotesnhacContext
, but the compiler says that one of these is missing.You shouldn't need to specify the type parameter at all; it will be inferred from the argument. You can just do:
...presuming you first fix the issue with the declaration.