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.

Related posts

Recent Viewed