Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

C# MVC with Entity Framework Database Initializer failing

Im working on a website as a leaning tool and have run into an issue where entity framework fails to set the database initializer. This is the error I am getting:

 System.InvalidOperationException
 HResult=0x80131509
 Message=Failed to set database initializer of type 'WhatsInStorage.DAL.DatabaseInitializer, WhatsInStorage' for DbContext type 'WhatsInStorage.DAL.DatabaseContext, WhatsInStorage' specified in the application configuration. See inner exception for details.
Source=<Cannot evaluate the exception source>
StackTrace:
<Cannot evaluate the exception stack trace>

Inner Exception 1:
TypeLoadException: Could not load type 
'WhatsInStorage.DAL.DatabaseInitializer' from assembly 'WhatsInStorage'.

To create my controller, I added a new controller with MVC5 Controller with views, using Entity Framework. As the model class, I used Box (it showed up in the dropdown) and for the Data context class, I used DatabaseContext (prepolulated).

This is model Box.cs:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

 using System.Collections.Generic;
 using System.ComponentModel.DataAnnotations;

 namespace WhatsInStorage.Models
 {
     public class Box
     {
         [Key]
         public int BoxId { get; set; }
         public int RoomNumber { get; set; }
         public List<Item> Items { get; set; }
     }
 }

This is the model Item.cs:

 using System.ComponentModel.DataAnnotations;

 namespace WhatsInStorage.Models
 {
     public class Item
     {
         [Key]
         public int BoxID {get; set;}
         public string ItemName { get; set; }
         public int Quantity { get; set; }

     }
 }

This is the code in my DatabaseContext.cs:

 using System.Data.Entity;
 using System.Data.Entity.ModelConfiguration.Conventions;
 using WhatsInStorage.Models;

 namespace WhatsInStorage.DAL
 {
     public class DatabaseContext : DbContext
     {
         public DatabaseContext() : base("DatabaseContext") 
         {
         }

         public DbSet<Rooms> Rooms { get; set; }
         public DbSet<Item> Item { get; set; }
         public DbSet<Box> Box { get; set; }
     }
 }

This is the code in DatabaseInitalizer.cs:

 using System.Collections.Generic;
 using WhatsInStorage.Models;

 namespace WhatsInStorage.DAL
 {
     public class DatabaseInitalizer : System.Data.Entity.DropCreateDatabaseIfModelChanges<DatabaseContext>
     {
         protected override void Seed(DatabaseContext context)
         {
             var InitRoom = new List<Rooms>()
             {
                 new Rooms{RoomID=1, RoomName="Bedroom 1"}
             };
             InitRoom.ForEach(s => context.Rooms.Add(s));
             context.SaveChanges();

             var InitBox = new List<Box>()
             {
                 new Box{BoxId=0, RoomNumber=0,Items=null}
             };
             InitBox.ForEach(s => context.Box.Add(s));
             context.SaveChanges();


             var InitItem = new List<Item>()
             {
                 new Item{BoxID=0, ItemName="", Quantity=0}
             };
             InitItem.ForEach(s => context.Item.Add(s));
             context.SaveChanges();
         }
     }
 }

This is the code in my web.config file:

   <entityFramework>
        <contexts>
             <context type="WhatsInStorage.DAL.DatabaseContext, WhatsInStorage">
                  <databaseInitializer type="WhatsInStorage.DAL.DatabaseInitializer, WhatsInStorage" />
             </context>
       </contexts>
   </entityFramework>
   <connectionStrings>
        <add name="DatabaseContext" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;Initial Catalog=WhatsInStorage1;Integrated Security=SSPI;" providerName="System.Data.SqlClient"/>
   </connectionStrings>

Any ideas as to where I am going wrong?
Thanks!

>Solution :

You have a typo where you are creating the initializer class. Here: DatabaseInitalizer

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading