开发者

EF 4.1 RC - 'System.Data.Entity.IDatabaseInitializer`1[TContext]' violates the constraint of type parameter 'TContext'

开发者 https://www.devze.com 2023-02-19 20:16 出处:网络
I\'ve just updated my project (using NuGet) to Entity Framework 4.1 RC and receives this error msg: GenericArguments[0],

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

Stack 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:116

Version 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. He开发者_JAVA技巧re'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.


The problem seems to be the line:

System.Data.Entity.Database.SetInitializer<NotesnhacContext>(new ContextInitializer());

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:

System.Data.Entity.Database.SetInitializer(new ContextInitializer());

...presuming you first fix the issue with the declaration.

0

精彩评论

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