我正在學習ASP.NET MVC5來創建一個網站。我在模型文件夾中創建了幾個類,並使用Entity Framework中的「腳手架特徵」來創建CRUD頁面。主頁中的一切都很好。但是,當我點擊導航欄中的「員工」鏈接,出現在這一行錯誤:未能設置數據庫初始化程序
public ActionResult Index()
{
return View(db.Employees.ToList());
}
的錯誤是:
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.
這裏是Employee
類:
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; }
}
部分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>
這裏有什麼錯?我一直在挖掘很長一段時間,但找不到原因。
更新
內部異常:
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:
查看內部異常的消息,併發帖 –
@MarcCals謝謝。剛剛發佈 – Searene