ASP .NET Entity Framework Database not initialized

asp.net-mvc entity-framework entity-framework-6 localdb

Question

I have created an ASP .NET MVC application with simple login and sign up functionality, and am trying to create an Entity Framework 6 model using Code First Approach. I am using LocalDb to create the database. However, I get the following exception when I add

<contexts>
  <context type="WebApplication1.DAL.ProjectContext, WebApplication1">
    <databaseInitializer type="WebApplication1.DAL.ProjectInitializer, WebApplication1"/>
  </context>
</contexts>

to the Web.config file. The exception is below:

Most of stack trace is:

[TypeLoadException: Could not load type 'WebApplication1.DAL.ProjectContext' from assembly 'WebApplication1'.]
System.RuntimeTypeHandle.GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMarkHandle stackMark, IntPtr pPrivHostBinder, Boolean loadTypeFromPartialName, ObjectHandleOnStack type) +0
   System.RuntimeTypeHandle.GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean loadTypeFromPartialName) +106
   System.RuntimeType.GetType(String typeName, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMark& stackMark) +62
   System.Type.GetType(String typeName, Boolean throwOnError) +46
   System.Data.Entity.Internal.InitializerConfig.TryGetInitializer(Type requiredContextType, String contextTypeName, String initializerTypeName, Boolean isDisabled, Func`1 initializerArgs, Func`3 exceptionMessage) +47

[InvalidOperationException: Failed to set database initializer of type 'WebApplication1.DAL.ProjectInitializer, WebApplication1' for DbContext type 'WebApplication1.DAL.ProjectContext, WebApplication1' specified in the application configuration. See inner exception for details.]
   System.Data.Entity.Internal.InitializerConfig.TryGetInitializer(Type requiredContextType, String contextTypeName, String initializerTypeName, Boolean isDisabled, Func`1 initializerArgs, Func`3 exceptionMessage) +327
   System.Data.Entity.Internal.<>c__DisplayClass6.<TryGetInitializerFromEntityFrameworkSection>b__1(ContextElement e) +276
   System.Linq.WhereSelectEnumerableIterator`2.MoveNext() +223

Here is the ProjectContext.cs code:

namespace WebApplication1.Models.DAL{
using System;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using WebApplication1.Models;
using WebApplication1.Models.DAL;

public partial class ProjectContext : DbContext
{
    public ProjectContext(): base()
    {

    }
    public DbSet<PIP> PIP { get; set; }
    public DbSet<Project> Project { get; set; }

}
}

Here is the ProjectInitializer.cs code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using WebApplication1.Models;

namespace WebApplication1.Models.DAL
{
    public class ProjectInitializer:System.Data.Entity.DropCreateDatabaseIfModelChanges<ProjectContext>
    {
        protected override void Seed(ProjectContext context)
        {
            var projects = new List<Project>
            {
                new Project{ ProjectID=1, Name="New ADM", StartDate=new DateTime(2017,1,10), EndDate=new DateTime(2017,2,10),},
                new Project{ ProjectID=2, Name="New ADM", StartDate=new DateTime(2016,5,20), EndDate=new DateTime(2016,6,20),},
                new Project{ ProjectID=3, Name="New ADM", StartDate=new DateTime(2015,8,30), EndDate=new DateTime(2015,9,30),}
            };

            projects.ForEach(pr => context.Project.Add(pr));
            context.SaveChanges();

            var PIP = new List<PIP>
            {
                new PIP{ PIPID=1, ProjectID=1 },
                new PIP{ PIPID=2, ProjectID=2 },
                new PIP{ PIPID=3, ProjectID=3 },
            };
            PIP.ForEach(pip => context.PIP.Add(pip));
            context.SaveChanges();

            Database.SetInitializer<ProjectContext>(new ProjectInitializer());

        }
    }
}
1
0
7/8/2018 5:11:46 PM

Popular Answer

You are using wrong namespace for ProjectContext and ProjectInitializer classes in web.config, it should be WebApplication1.Models.DAL instead of WebApplication1.DAL.

<contexts>
  <context type="WebApplication1.Models.DAL.ProjectContext, WebApplication1">
    <databaseInitializer type="WebApplication1.Models.DAL.ProjectInitializer, WebApplication1"/>
  </context>
</contexts>
0
7/8/2018 6:36:24 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow