简体   繁体   中英

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:

 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!

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

Cannot comment yet, but can you share your Rooms class?

Also, I don't think EF lets you specify the Key ID when adding it into the Database. So try to only add the data without the ID .

Example:

var InitItem = new List<Item>()
{
    new Item{ItemName="", Quantity=0}
};

InitItem.ForEach(s => context.Item.Add(s));
context.SaveChanges();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM