1

I'm learning ASP.NET MVC5 to make a website. I created several classes in the model folder and used "scaffolding feature" in Entity Framework to create CRUD pages. Everything is fine in the Home page. But when I clicked the "Employee" link in the navigation bar, there was an error in this line:

public ActionResult Index() { return View(db.Employees.ToList()); } 

The error is:

An exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll but was not handled in user code Additional information: Failed to set database initializer of type 'wms.DAL.ProductInitilizer, wms' for DbContext type 'wms.DAL.ProductContext, wms' specified in the application configuration. See inner exception for details. 

Here is Employee class:

public class Employee { public string ID { get; set; } public string Name { get; set; } public string Department { get; set; } public string Post { get; set; } public string PS { get; set; } public virtual ICollection<Purchase> Purchases { get; set; } public virtual ICollection<Delivery> Deliveries { get; set; } public virtual ICollection<Stock> Stocks { get; set; } } 

part of ProductInitializer:

namespace wms.DAL { public class ProductInitializer : System.Data.Entity.DropCreateDatabaseAlways<ProductContext> { protected override void Seed(ProductContext context) { ... var employees = new List<Employee> { new Employee{ID="238047AF1",Name="Bill",Department="Storage",Post="worker",PS="default"}, new Employee{ID="2457656AB",Name="Bob",Department="Storage",Post="worker",PS="default"} }; employees.ForEach(s => context.Employees.Add(s)); context.SaveChanges(); } } } 

Web.config:

<?xml version="1.0"?> <configuration> <configSections> <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"> <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" /> <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" /> </sectionGroup> </configSections> <system.web.webPages.razor> <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.2.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> <pages pageBaseType="System.Web.Mvc.WebViewPage"> <namespaces> <add namespace="System.Web.Mvc" /> <add namespace="System.Web.Mvc.Ajax" /> <add namespace="System.Web.Mvc.Html" /> <add namespace="System.Web.Optimization"/> <add namespace="System.Web.Routing" /> <add namespace="wms" /> </namespaces> </pages> </system.web.webPages.razor> <appSettings> <add key="webpages:Enabled" value="false" /> </appSettings> <system.webServer> <handlers> <remove name="BlockViewHandler"/> <add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" /> </handlers> </system.webServer> </configuration> 

What's wrong here? I've been digging for a long time but couldn't find the reason.


Update

inner exception:

System.InvalidOperationException was unhandled by user code HResult=-2146233079 Message=Failed to set database initializer of type 'wms.DAL.ProductInitilizer, wms' for DbContext type 'wms.DAL.ProductContext, wms' specified in the application configuration. See inner exception for details. Source=EntityFramework StackTrace: System.Data.Entity.Internal.InitializerConfig.TryGetInitializer(Type requiredContextType, String contextTypeName, String initializerTypeName, Boolean isDisabled, Func`1 initializerArgs, Func`3 exceptionMessage) System.Data.Entity.Internal.InitializerConfig.<>c__DisplayClass6.<TryGetInitializerFromEntityFrameworkSection>b__1(ContextElement e) System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext() System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source, Func`2 predicate) System.Data.Entity.Internal.InitializerConfig.TryGetInitializerFromEntityFrameworkSection(Type contextType) System.Data.Entity.Internal.InitializerConfig.TryGetInitializer(Type contextType) System.Data.Entity.Infrastructure.DependencyResolution.AppConfigDependencyResolver.GetServiceFactory(Type type, String name) System.Data.Entity.Infrastructure.DependencyResolution.AppConfigDependencyResolver.<>c__DisplayClass1.<GetService>b__0(Tuple`2 t) System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory) System.Data.Entity.Infrastructure.DependencyResolution.AppConfigDependencyResolver.GetService(Type type, Object key) System.Data.Entity.Infrastructure.DependencyResolution.ResolverChain.<>c__DisplayClass3.<GetService>b__0(IDbDependencyResolver r) System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext() System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source, Func`2 predicate) System.Data.Entity.Infrastructure.DependencyResolution.ResolverChain.GetService(Type type, Object key) System.Data.Entity.Infrastructure.DependencyResolution.CompositeResolver`2.GetService(Type type, Object key) System.Data.Entity.Infrastructure.DependencyResolution.DbDependencyResolverExtensions.GetService(IDbDependencyResolver resolver, Type type) System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization() System.Data.Entity.Internal.LazyInternalContext.<InitializeDatabase>b__4(InternalContext c) System.Data.Entity.Internal.RetryAction`1.PerformAction(TInput input) System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action`1 action) System.Data.Entity.Internal.LazyInternalContext.InitializeDatabase() System.Data.Entity.Internal.InternalContext.Initialize() System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) System.Data.Entity.Internal.Linq.InternalSet`1.Initialize() System.Data.Entity.Internal.Linq.InternalSet`1.GetEnumerator() System.Data.Entity.Infrastructure.DbQuery`1.System.Collections.Generic.IEnumerable<TResult>.GetEnumerator() System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source) wms.Controllers.EmployeeController.Index() Location c:\Users\cityofsky\Documents\Visual Studio 2013\Projects\wms\wms\Controllers\EmployeeController.cs:Line 21 lambda_method(Closure , ControllerBase , Object[] ) System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) System.Web.Mvc.Async.AsyncControllerActionInvoker.ActionInvocation.InvokeSynchronousActionMethod() System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState) System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult) System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End() System.Web.Mvc.Async.AsyncResultWrapper.End[TResult](IAsyncResult asyncResult, Object tag) System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d() System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() InnerException: System.TypeLoadException HResult=-2146233054 Message=Could not load type "wms.DAL.ProductInitilizer" from assembly "wms" Source=mscorlib TypeName=wms.DAL.ProductInitilizer StackTrace: System.RuntimeTypeHandle.GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMarkHandle stackMark, IntPtr pPrivHostBinder, Boolean loadTypeFromPartialName, ObjectHandleOnStack type) System.RuntimeTypeHandle.GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean loadTypeFromPartialName) System.RuntimeType.GetType(String typeName, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMark& stackMark) System.Type.GetType(String typeName, Boolean throwOnError) System.Data.Entity.Internal.InitializerConfig.TryGetInitializer(Type requiredContextType, String contextTypeName, String initializerTypeName, Boolean isDisabled, Func`1 initializerArgs, Func`3 exceptionMessage) InnerException: 
2
  • Check the message of the inner exception, and post it Commented Jun 12, 2015 at 9:20
  • @MarcCals Thank you. Posted just now Commented Jun 12, 2015 at 9:33

1 Answer 1

2

You posted the wrong web.config. Post the one in the root of your project. Though the database initializer can also be inside your global.asax.

But what i see is that the exception refers to 'ProductInitilizer' while the name of the class with the initializer is 'ProductInitializer'. Typo is the problem?

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, typo is the problem, you are so amazing. Thank you!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.